testNG之异常测试
@Test(expectedExceptions = )
在测试的时候,某些用例的输入条件,预期结果是代码抛出异常,那么这个时候就需要testNG的异常测试,先看一段会抛出异常的代码
exception.java:
import org.testng.annotations.Test; public class exception { @Test public void testMethod() { int a = 0; int b = 10; System.out.println(b / a); } }
运行结果:
FAILED: testMethod java.lang.ArithmeticException: / by zero at exception.testMethod(exception.java:9) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74) at org.testng.internal.Invoker.invokeMethod(Invoker.java:673) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.runWorkers(TestRunner.java:1147) at org.testng.TestRunner.privateRun(TestRunner.java:749) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:317) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274) at org.testng.SuiteRunner.run(SuiteRunner.java:223) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039) at org.testng.TestNG.runSuitesLocally(TestNG.java:964) at org.testng.TestNG.run(TestNG.java:900) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174) =============================================== Default test Tests run: 1, Failures: 1, Skips: 0 ===============================================
如果我们的预期结果就是代码抛出异常ArithmeticException,用例通过的话,则通过@Test的expectedExceptions属性,通过该属性指定抛出异常的类,
修改exception.java,添加(expectedExceptions = ArithmeticException.class):
import org.testng.annotations.Test; public class exception { @Test(expectedExceptions = ArithmeticException.class) public void testMethod() { int a = 0; int b = 10; System.out.println(b / a); } }
再次运行,结果通过:
PASSED: testMethod =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 ===============================================
如果是一个正常的代码,期望抛出异常的话,会怎样呢?
修改exception.java,不会抛出异常:
import org.testng.annotations.Test; public class exception { @Test(expectedExceptions = ArithmeticException.class) public void testMethod() { int a = 0; int b = 10; System.out.println(a / b); } }
再次运行,结果失败:
FAILED: testMethod org.testng.TestException: Expected exception java.lang.ArithmeticException but got org.testng.TestException: Method exception.testMethod() should have thrown an exception of class java.lang.ArithmeticException at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1416) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1184) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) at org.testng.TestRunner.runWorkers(TestRunner.java:1147) at org.testng.TestRunner.privateRun(TestRunner.java:749) at org.testng.TestRunner.run(TestRunner.java:600) at org.testng.SuiteRunner.runTest(SuiteRunner.java:317) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274) at org.testng.SuiteRunner.run(SuiteRunner.java:223) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039) at org.testng.TestNG.runSuitesLocally(TestNG.java:964) at org.testng.TestNG.run(TestNG.java:900) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174) Caused by: org.testng.TestException: Method exception.testMethod() should have thrown an exception of class java.lang.ArithmeticException at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1442) at org.testng.internal.Invoker.invokeMethod(Invoker.java:722) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170) ... 17 more =============================================== Default test Tests run: 1, Failures: 1, Skips: 0 ===============================================