net自动化测试之道API测试-测试抛出异常的方法

来源:互联网 发布:linux管理系统何明课件 编辑:程序博客网 时间:2024/05/16 05:31

Dealing with Methods that Throw Exceptions

测试抛出异常的方法

Problem

You want to test a method that throws anexception.

问题

如何测试一个抛出异常的方法呢?

Design

Embed a special string token in your testcase data file to signal that an exception should be

thrown,and place the call to the methodunder test in a try block so you can catch the excep-

tion if it is thrown.

设计

在测试用例文件中用一个特殊的标记字符串标识方法会抛出异常,并且将调用方法的代码放在try代码块中,这样如果方法抛出异常将会被捕获。

解决方案

增加一个预期结果为“Exception”的测试用例:

0004:GeometricMean:1 2 4 8 16 32:6.6569

0005:GeometricMean:0 0 0 0:Exception

0006:GeometricMean:2 4 8:4.0000

然后在测试主程序的循环中处理:

MathLib.Methods m=new MathLib.Methods();

if(tokens[3]=="Exception")

{

try

{

actual=m.GeometricMean(input);

}

catch(Exception ex)

{

Console.WriteLine(caseID+"Pass");

continue;

}

Console.WriteLine(caseID+"*FAIL*noexception thrown");

}

else

{

//use regular test logic

}

Comments

A common situation is that methods willthrow an exception for certain input.For example,a

method that performs a division operationwith one of its input arguments may throw an excep-

tion if the value of the argument is 0.Inthis example,we assume the original GeometricMean()

method has been modified so that passingall zero values to the method throws an exception by

design.We check for this special input byexamining the test case data for an“Exception”string.

If we find it,we branch to code that wrapsthe call to GeometricMean()in a try block.If an excep-

tion is thrown as expected,control istransferred to the catch block,and we print a pass result.

Then we move to the next test case when thecontinue statement is executed.If calling

GeometricMean()does not throw an exception,controlwill reach the

Console.WriteLine(caseID+"*FAIL*noexception thrown");

statement.Notice you do not want to wrapcalls to the method under test that do not throw an

exception in a try block because if themethod does throw an exception,you’ll get a pass result.

Dealing with methods that throw anexception can be messy in terms of integrating that special

logic into the“regular”logic of your testharness.Because of this,a good strategy is to create

two different lightweight testharnesses—one harness for test cases that do not throw excep-

tions and one harness just for cases thatdo.

The preceding solution is designed so thatwe test only that an exception is thrown,not

necessarily a particular exception.In somesituations,you may want to check for a specific

exception.One way to do this is to embedthe exception message in your test case file and

check for it in your harness logic.Forexample,suppose the GeometricMean()method contains

this code:

if(denominator==0)

throw new Exception("Invalid division");

You could create this test case:

0005:GeometricMean:0 0 0 0:Invalid division

and then test inside the main test looplike this:

expected=tokens[3];//"Invaliddivision"

try

{

actual=m.GeometricMean(input);

}

catch(Exception ex)

{

if(ex.Message==expected)

{

Console.WriteLine(caseID+"Pass;correctexception");

continue;

}

else

{

Console.WriteLine(caseID+"*FAIL*;wrongexception");

}

}

Console.WriteLine(caseID+"*FAIL*;noexception thrown");

注解

对于某些输入,方法会抛出异常,这是一种很常见的情形。例如,对于执行除法操作的方法,如果作为除数的参数值为0就会抛出异常。在我们这个例子中,假设将原来的GeometricMean方法修改为如果所有的参数值都为0,方法将抛出异常。我们通过测试用例中“Exception”字符串识别方法是否抛出异常。如果找到这样的方法,控制将进入包括try块的分支中,该try块中调用了被测方法,如果正如预期那样,方法抛出异常,则进入catch块,打印成功的结果。然后当continue语句执行后,我们移到下一个测试用例。如果调用GeometricMean方法没有抛出异常,则执行语句:

Console.WriteLine(caseID+"*FAIL*noexception thrown");

注意,不会抛出异常的方法的调用不要放到try语句中。因为如果方法抛出异常,我们将获得测试通过的信息。将处理抛出异常方法的这类特殊逻辑放到正常的逻辑中会造成混乱。因此,一种好的策略就是创建两个不同的轻量级测试套件,一个用例处理抛出异常的测试用例,另一个处理其他的。

前面的解决方案设计为仅仅测试出抛出了异常,但是不知具体是何种异常。在一些场景中,我们可能想检查特定的异常。要做到这样一种方式是将异常信息嵌入到测试用例中,并且在测试套件中检查。例如,假设GeometricMean方法包含下面的代码:

if(denominator==0)

throw new Exception("Invaliddivision");

那么我们应该这样创建测试用例:

0005:GeometricMean:0 0 0 0:Invalid division

然后测试代码这样:

expected=tokens[3];//"Invaliddivision"

try

{

actual=m.GeometricMean(input);

}

catch(Exception ex)

{

if(ex.Message==expected)

{

Console.WriteLine(caseID+"Pass;correctexception");

continue;

}

else

{

Console.WriteLine(caseID+"*FAIL*;wrongexception");

}

}

Console.WriteLine(caseID+"*FAIL*;noexception thrown");

原创粉丝点击