Logging Test Case Results
存储测试用例执行结果
Problem
You want to save test case results toexternal storage as a simple text file.
问题
如果我们想将测试用例结果存储在外部文本文件中,该如何实现呢?
Design
Inside the main test case processingloop,use a System.IO.StreamWriter object to write a test
case ID and a pass or fail result.
设计
将主要的处理测试用例的语句放到循环中,使用System.IO.StreamWriter对象将测试用例ID、测试用例结果写到外部文件中。
解决方案
//open StreamReader sr here
FileStream ofs=newFileStream("..\\..\\TestResults.txt",
FileMode.CreateNew);
StreamWriter sw=new StreamWriter(ofs);
string line,caseID,method,expected;
double actual=0.0;
while((line=sr.ReadLine())!=null)
{
//parse"line"here
if(method=="ArithmeticMean")
{
actual=MathLib.Methods.ArithmeticMean(input);
if(actual.ToString("F4")==expected)
sw.WriteLine(caseID+"Pass");
else
sw.WriteLine(caseID+"*FAIL*");
}
else
{
sw.WriteLine(caseID+"Unknownmethod");
}
}//while
sw.Close();
ofs.Close();
Comments
In many situations,you’ll want to writeyour test case results to external storage instead of,or
in addition to,displaying them in thecommand shell.The simplest form of external storage is
a text file.Alternatives include writing toa SQL table or an XML file.You create a FileStream
object and a StreamWriter object to writetest case results to external storage.In this solution,
the FileMode.CreateNew argument creates anew text file named TestResults.txttwo directo-
ries above the test harnessexecutable.Using a relative file path allows you to move your entire
test harness directory structure ifnecessary.Then you can use the StreamWriter object to
write test results to external storage justas you would to the console.
When passing in aFileMode.CreateNew“TestResults.txt”argument,if a file with the
name TestResults.txtalready exists,an exception will be thrown.You can avoid this by using
a FileMode.Createargument,but then any existing TestResults.txt filewill be overwritten,
and you could lose test results.Onestrategy is to parameterize the test results file name
static void Main(string[]args)
{
string testResultsFile=args[0];
FileStream ofs=newFileStream(testResultsFile,
FileMode.CreateNew);
StreamWriter sw=new StreamWriter(ofs);
//etc.
}
and pass in a new manually generated testresults file name for each run:
C:\Harness\bin\Debug>Run.exeResults-12-25-06.txt
Alternatives include writing results to aprogrammatically time-stamped file.
Our examples so far have either writtentest results to the command shell or to a.txt file,
but you can write results to both consoleand external storage:
if(actual.ToString("F4")==expected)
{
Console.WriteLine(caseID+"Pass");
sw.WriteLine(caseID+"Pass");
}
else
{
Console.WriteLine(caseID+"*FAIL*");
sw.WriteLine(caseID+"*FAIL*");
}
When the StreamWriter.WriteLine()statementexecutes,it does not actually write results to
your output file.Results are buffered andthen flush out only when the StreamWriter.Close()
statement executes.You can force results tobe written by explicitly issuing a StreamWriter.Flush()
statement.This is usually most importantwhen you have a lot of test cases or when you catch an
exception—be sure to close any open streamsin either the catch block or the finally block so that
buffered results will be written to fileand not lost:
catch(Exception ex)
{
Console.WriteLine("Unexpectedfatal error:"+ex.Message);
sw.Close();
//close other open streams
}
注解
很多时候,我们想把测试结果写到外部文件中,而不是显示在控制台。最简单的外部存储介质就是文本文件,当然我们可以写到数据库或XML文件中。我们创建一个FileStream对象和一个StreamWriter对象来将测试用例结果写到外部存储介质中。在这个解决方案中,我们在测试套件的执行目录的上两级目录中创建了一个叫TestResults.txt 的文件,FileMode.CreateNew参数表示创建一个新的文件。使用相对目录有利于我们移动整个测试套件。然后我们使用StreamWriter对象将结果写到外部文件中。FileMode.CreateNew参数表示创建一个新的文件,如果TestResults.txt文件已经存在,则会抛出异常。我们可以使用FileMode.Create参数,但是这样就会重写任何一个已经存在的TestResults.txt,已有的测试结果将丢失。解决的这种问题的一种策略是参数化存储测试结果的文件名。如:
static void Main(string[]args)
{
string testResultsFile=args[0];
FileStream ofs=newFileStream(testResultsFile,
FileMode.CreateNew);
StreamWriter sw=new StreamWriter(ofs);
//etc.
}
并且每次运行测试套件的时候传入文件名:
C:\Harness\bin\Debug>Run.exeResults-12-25-06.txt
另外一种方式使让程序自动将结果写到时间戳文件中。
到目前为止,我们的例子要么将结果显示在控制台,要么写到文本文件中,当然我们也可以将结果既显示到控制台,又存储到外部文件中:
if(actual.ToString("F4")==expected)
{
Console.WriteLine(caseID+"Pass");
sw.WriteLine(caseID+"Pass");
}
else
{
Console.WriteLine(caseID+"*FAIL*");
sw.WriteLine(caseID+"*FAIL*");
}
当StreamWriter.WriteLine()语句执行后,实际上它并没有将结果写到我们的输出文件中,而是在缓存中,只有当StreamWriter.Close()语句执行后才被写入到输出文件。我也可以使用StreamWriter.Close()显示地将结果写入输出文件。当我们有大量的测试用例并且使用了catch语句的时候,确保打开的流在catch块和finally块中关闭了,保证缓存中的测试结果被写入到了文件中而没丢失:
catch(Exception ex)
{
Console.WriteLine("Unexpected fatalerror:"+ex.Message);
sw.Close();
//close other open streams
}