预计测试会出现异常,可以使用@Test(expectedExceptions=)来验证是否有异常抛出。
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestException {
@Test(description="测试异常",dataProvider="data", expectedExceptions=ArithmeticException.class)
public void TestDataOut(int a,int b) {
int result=a+b;
System.out.println("result:"+result);
Reporter.log("测试结果:"+result);
}
@DataProvider(name="data")
public Object[][]dataprovide(){
return new Object[][] {
new Object[] {111111,1111111111},
new Object[] {4,2147483647},
};
}
}
测试结果

Maven Surefire Plugin参看http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html
本文介绍了一种使用 TestNG 框架进行单元测试的方法,通过 @Test 注解的 expectedExceptions 属性来验证预期的异常是否被正确抛出。示例代码展示了如何设置数据提供者以测试不同的输入情况。
1166

被折叠的 条评论
为什么被折叠?



