14、自动运行测试套件
用.BAT文件可以实现,用System.Diagnosis.Process命名空间下的Start()方法
在这里,稍微实现了代码
1 using System; 2 using System.IO; 3 4 namespace ApiTestExample 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 FileStream ifs=null; 11 StreamReader sr = null; 12 StreamWriter sw = null; 13 FileStream fs = null; 14 try 15 { 16 ifs = new FileStream("E:\\C#\\C#.NET例子程序\\testcase.txt", FileMode.Open); 17 sr = new StreamReader(ifs); 18 string stamp = DateTime.Now.ToString(); 19 stamp = stamp.Replace(":","-");//这里不进行符号替换,创建不来文件,以为:这个符号不能用于创建文件问 20 fs=File.Create("E:\\C#\\C#.NET例子程序\\"+stamp +" testresult.txt"); 21 sw = new StreamWriter(fs); 22 string line, caseID, method; 23 string[] tokens, tempInput; 24 string expected; 25 double actual = 0.0; 26 int numPass = 0, numFail = 0; 27 28 Console.WriteLine("\nCaseID Result Method Details"); 29 Console.WriteLine("==============================="); 30 31 while ((line =sr.ReadLine())!=null) 32 { 33 tokens = line.Split(':'); 34 caseID =tokens [0]; 35 method =tokens[1]; 36 tempInput =tokens[2].Split (' '); 37 expected=tokens [3]; 38 39 int[] input=new int[tempInput.Length ]; 40 for (int i = 0; i < input.Length ; i++) 41 { 42 input[i] = int.Parse(tempInput [i]); 43 } 44 45 if (method == "ArihthmeticMean") 46 { 47 actual = ArihthmeticMean(input[0], input[1]); 48 if (actual.ToString() == expected) 49 { 50 Console.WriteLine(caseID + " Pass " + method + " actual=" + actual.ToString()); 51 //记录测试 52 sw.WriteLine(caseID + " Pass " + method + " actual=" + actual.ToString()); 53 ++numPass; 54 } 55 else 56 { 57 Console.WriteLine(caseID + " Fail " + method + " actual=" + actual.ToString()); 58 //记录测试 59 sw.WriteLine(caseID + " Fail " + method + " actual=" + actual.ToString()); 60 ++numFail; 61 } 62 63 } 64 else 65 { 66 Console.WriteLine(caseID +" "+method +"not yet implement"); 67 sw.WriteLine(caseID + " " + method + "not yet implement"); 68 } 69 70 } 71 72 Console.WriteLine("===========================end test run==============="); 73 Console.WriteLine("pass="+numPass +"Fail="+numFail ); 74 } 75 catch(Exception ex) 76 { 77 Console.WriteLine("Fatal error"+ex.Message ); 78 } 79 finally 80 { 81 //注意打开的文件资源一定要关闭,还有注意顺序 82 ifs.Close(); 83 sr.Close(); 84 sw.Close(); 85 fs.Close(); 86 87 88 } 89 90 91 } |