您的位置:首页 > 其它

net自动化测试之道API测试-统计测试结果

2011-08-12 22:20 399 查看
Calculating Summary Results

统计测试结果

Problem

You want to tally your test case results totrack the number of test cases that pass and the

number of cases that fail.

问题

如何统计通过测试的测试用例和失败的用例呢?

Design

Use simple integer counters initialized to0 at the beginning of each test run.

设计

设置用于计数的整型变量,初始值为0.

解决方案

int numPass=0,numFail=0;

while((line=sr.ReadLine())!=null)

{

//parse"line"here

if(method=="ArithmeticMean")

{

actual=MathLib.Methods.ArithmeticMean(input);

if(actual.ToString("F4")==expected)

{

Console.WriteLine("Pass");

++numPass;

}

else

{

Console.WriteLine("*FAIL*");

++numFail;

}

}

else

{

Console.WriteLine("Unknownmethod");

//no effect on numPass or numFail

}

}//loop

Console.WriteLine("Number casespassed="+numPass);

Console.WriteLine("Number casesfailed="+numFail);

Console.WriteLine("Totalcases="+(numPass+numFail));

doublepercent=((double)numPass)/(numPass+numFail);

Console.WriteLine("Percentpassed="+percent.ToString("P"));

Comments

It is often useful to calculate and recordsummary metrics such as the total number of testcases

that pass and the number that fail.If youtrack these numbers daily,you can gauge the progress

of the quality of your software system.Youmight also want to record and track the percentage

of test cases that pass because mostproduct specifications have exit criteria such as,“formile-

stone MM3,a full API testpass will achieve a 99.95%test case pass rate.”You can declare

integer variables and initialize them to 0outside the main test loop.If a test case passes,you

increment the pass counter;if the test casefails,you increment the fail counter.After all tests

have been run,you can display your summarymetrics and/or write them to external storage.

Console.WriteLine("Warning:Counterlogic failure");

In the preceding solution we track thenumber of test cases that pass and the number that

fail and then add them to determine thetotal number of cases run.You may also want to ini-

tialize and insert a counter numCasesthat increments after every test case so you can verify

your test harness logic:

if(actual.ToString("F4")==expected)

{

Console.WriteLine("Pass");

++numPass;

++numCases;

}

else

{

Console.WriteLine("*FAIL*");

++numFail;

++numCases;

}

//etc.

if((numPass+numFail)!=numCases)

Console.WriteLine("Warning:Counterlogic failure");

When calculating a percent pass rate,becareful to cast either the numerator or denomi-

nator to type double so that the result ofthe division operation is implicitly converted to type

double:

doublepercent=((double)numPass)/(numPass+numFail);

If you don’t cast,you will be performinginteger division and always get either 1.0(100%)

or 0.0(0%)for the result.Instead of usingan explicit C#cast to type double,you can perform

an implicit cast by multiplying by 1.0:

doublepercent=(numPass*1.0)/(numPass+numFail);

This old technique has the advantage ofbeing language-independent but the disadvantage

of doing more work than is necessary.

注解

统计并记录简要的度量值如通过测试的测试用例总数和失败的测试用例总数通常很有用。如果我们每日跟踪这些值,会帮助我们估计软件系统的质量的进展情况。我们也可能需要记录和跟踪通过测试的测试用例所占的比率,因为很多产品说明书中有类似这样的退出标准“到里程碑MM3,整个API测试测试用例通过率达到99.95%”。我们可以在主要的测试循环体外声明几个整型变量,并且初始化为0.如果一个测试用例通过测试了,我们就给通过计数器加1,如果测试用例失败,则给失败计数器加1.在所有的测试用例都执行后,我们将统计结果显示到外部文件中。在上面的解决方案中,我们统计通过测试的用例个数和失败的用例个数,然后相加得到运行的测试用例总数。也可以增加一个计数器numCases来统计测试用例总数,每运行一个用例numCases就加1,如果要这样,测试套件的逻辑要稍微修改下:

if(actual.ToString("F4")==expected)

{

Console.WriteLine("Pass");

++numPass;

++numCases;

}

else

{

Console.WriteLine("*FAIL*");

++numFail;

++numCases;

}

//etc.

if((numPass+numFail)!=numCases)

在计算用例通过率的时候,注意要将分子或者分母转换为double类型的,这样相除的结果会隐式转换为double类型:

doublepercent=((double)numPass)/(numPass+numFail);

如果不转换,就会以整型相差,并且结果总是1.0(100%)或0.0(0%),不使用显式转换为double的方式,我们可以通过乘以1.0隐式转换为double类型的:

double percent=(numPass*1.0)/(numPass+numFail);

第一种方式的优点是与语言无关,但是缺点是比实际多做了些工作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: