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:softwareInstallMenujavajdk1.8in...
这是一个失败的异常测试

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
原文地址:https://www.cnblogs.com/janson071/p/10003165.html