TestNG異常測試


用@Test(expectedExceptions = xxx) 聲明

package com.janson;

import org.testng.annotations.Test;

public class ExpectedException {
    /**
     * 什么時候會用到異常測試?
     * 在我們期望結果為某一個異常的時候
     * 比如:我們傳入了某些不合法的參數,程序拋出了異常
     * 也就是說我們的預期結果就是這個異常
     */

    //這是一個測試結果會失敗的異常測試
    @Test(expectedExceptions = RuntimeException.class)
    public void runTimeExceptionFailed() {
        System.out.println("這是一個失敗的異常測試");
    }

    //這是一個測試結果為成功的異常測試
    @Test(expectedExceptions = RuntimeException.class)
    public void runTimeExceptionSuccess() {
        System.out.println("這是一個成功的異常測試");
        throw new RuntimeException();
    }

    @Test(expectedExceptions = ArithmeticException.class)
    public void arithmeticException() {
        int i = 1/0;
        //System.out.println("After division the value of i is :" + i);
    }
}

runTimeExceptionFailed() 測試用例執行會報錯:

D:\softwareInstallMenu\java\jdk1.8\bin\...
這是一個失敗的異常測試

org.testng.TestException: 
Method ExpectedException.runTimeExceptionFailed()[pri:0, instance:com.ucar.ExpectedException@17ed40e0] should have thrown an exception of type class java.lang.RuntimeException

    at org.testng.internal.ExpectedExceptionsHolder.noException(ExpectedExceptionsHolder.java:89)
    at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1416)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:695)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:744)
    at org.testng.TestRunner.run(TestRunner.java:602)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
    at org.testng.TestNG.runSuites(TestNG.java:1144)
    at org.testng.TestNG.run(TestNG.java:1115)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)

runTimeExceptionSuccess()測試用例執行不會報錯:

這是一個成功的異常測試

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

arithmeticException()測試用例執行不會報錯:

===============================================
Default Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM