testNG异常测试
testNG跟踪异常处理代码提供了一个选项。可是测试需要代码抛出异常或不抛出。@Test注释expectedExceptions参数一起使用。@Test(expectedExceptions)
创建一个类
创建一个Java类进行测试
在printMessage()方法里添加一个错误条件
package hello;
/*
* This class prints the given message on console.
*/
public class MessageUtil {
private String message;
//Constructor
//@param message to be printed
public MessageUtil(String message){
this.message = message;
}
// prints the message
public void printMessage(){
System.out.println(message);
int a =0;
int b = 1/a;
a = b;
}
// add "Hi!" to the message
public String salutationMessage(){
message = "Hi!" + message;
System.out.println(message);
return message;
}
}
创建测试案例类
创建一个Java测试类为ExpectedExceptionTest.java
添加的ArithmeticExption和testPrintMessage()测试用例的预测异常
package hello;
import org.testng.Assert;
import org.testng.annotations.Test;
public class ExpectedExceptionTest {
String message = "Manisha";
MessageUtil messageUtil = new MessageUtil(message);
@Test(expectedExceptions = ArithmeticException.class)
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
messageUtil.printMessage();
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Manisha";
Assert.assertEquals(message,messageUtil.salutationMessage());
}
}
创建测试运行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<classes>
<class name="hello.ExpectedExceptionTest" />
</classes>
</test>
</suite>
测试结果
---------------------
【转载,仅作分享,侵删】
作者:若海音符
原文:https://blog.csdn.net/Chenyt01/article/details/51752762
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|