• 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
  • 相关阅读:
    Linux 下QT安装教程
    内核中断及按键驱动程序
    Linux 输入子系统原理理解(原创)
    深入分析Linux内核源码oss.org.cn/kernel-book/
    [MSDN]最新Win7 SP1简体中文所有版本下载
    递归打印级联目录
    递归与迭代学习(联级目录的创建与删除)
    PHP三种方法实现多文件上传
    PHP实现商城购物车类(SESSION+单例模式 )(亲测)
    PHP生成缩略图、验证码类封装
  • 原文地址:https://www.cnblogs.com/janson071/p/10003165.html
Copyright © 2020-2023  润新知