5、testNG異常處理

當程序出現異常或者測試中有異常測試案例可使他拋出異常java

 

例如:0不能夠當作除數,若是將除數設置爲0會拋出異常eclipse

在testNG上加上 expectedExceptions = ArithmeticException.class 測試

運行測試案例測試經過ui

 

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 ,會報出程序異常spa

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 彙整錯誤信息,方便觀察錯誤類、方法,方便回購測試3d

相關文章
相關標籤/搜索