當程式出現例外或者測驗中有例外測驗案例可以使他拋出例外
例如:0不可以當做除數,如果將除數設定為0會拋出例外
在testNG上加上 expectedExceptions = ArithmeticException.class
運行測驗案例測驗通過
package com.lc.testngYiChang; import org.testng.annotations.Test; public class testNG04 { @Test(expectedExceptions = ArithmeticException.class) public void testNG04_01() { int a = 0 ; int b = 1 ; System.out.println("我是testNG04類的testNG04——01方法,我在例外之前列印,"); System.out.println("我是b/a "+b/a); System.out.println("我是testNG04類的testNG04——01方法,我在例外之后列印,"); } }

如果程式正常,在測方法上加上 expectedExceptions = ArithmeticException.class ,會報出程式例外
package com.lc.testngYiChang; import org.testng.annotations.Test; public class testNG04 { @Test(expectedExceptions = ArithmeticException.class) public void testNG04_01() { int a = 0 ; int b = 1 ; System.out.println("我是testNG04類的testNG04——01方法,我在例外之前列印,"); System.out.println("我是b/a "+b/b); System.out.println("我是testNG04類的testNG04——01方法,我在例外之后列印,"); } }
[TestNG] Running: C:\Users\lc\AppData\Local\Temp\testng-eclipse-466935271\testng-customsuite.xml 我是testNG04類的testNG04——01方法,我在例外之前列印, 我是b/a 1 我是testNG04類的testNG04——01方法,我在例外之后列印, FAILED: testNG04_01 org.testng.TestException: Method testNG04.testNG04_01()[pri:0, instance:com.lc.testngYiChang.testNG04@256216b3] should have thrown an exception of type class java.lang.ArithmeticException at org.testng.internal.ExpectedExceptionsHolder.noException(ExpectedExceptionsHolder.java:89) at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1376) at org.testng.internal.Invoker.invokeMethod(Invoker.java:677) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) at org.testng.TestRunner.privateRun(TestRunner.java:774) at org.testng.TestRunner.run(TestRunner.java:624) at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at org.testng.SuiteRunner.run(SuiteRunner.java:261) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1191) at org.testng.TestNG.runSuitesLocally(TestNG.java:1116) at org.testng.TestNG.run(TestNG.java:1024) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176) =============================================== Default test Tests run: 1, Failures: 1, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 1, Skips: 0 =============================================== [TestNG] Time taken by org.testng.reporters.EmailableReporter2@61064425: 11 ms [TestNG] Time taken by org.testng.reporters.jq.Main@16f65612: 59 ms [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@6bc168e5: 5 ms [TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 5 ms [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@77556fd: 27 ms [TestNG] Time taken by org.testng.reporters.XMLReporter@7a7b0070: 3 ms
另外如果程式出現例外 會在專案下test-output/testng-failed.xml 匯整錯誤資訊,方便觀察錯誤類、方法,方便回購測驗

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/231345.html
標籤:其他
上一篇:weevely 常用命令講解
下一篇:作業中Linux常用命令
