@Test(expectedExceptions = )

  在测试的时候,某些用例的输入条件,预期结果是代码抛出异常,那么这个时候就需要testNG的异常测试,先看一段会抛出异常的代码

exception.java:

import org.testng.annotations.Test;

public class exception {
@Test
public void testMethod() {
int a = 0;
int b = 10;
System.out.println(b / a);
}
}

运行结果:

FAILED: testMethod
java.lang.ArithmeticException: / by zero
at exception.testMethod(exception.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:673)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1147)
at org.testng.TestRunner.privateRun(TestRunner.java:749)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:317)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274)
at org.testng.SuiteRunner.run(SuiteRunner.java:223)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039)
at org.testng.TestNG.runSuitesLocally(TestNG.java:964)
at org.testng.TestNG.run(TestNG.java:900)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174) ===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================

如果我们的预期结果就是代码抛出异常ArithmeticException,用例通过的话,则通过@Test的expectedExceptions属性,通过该属性指定抛出异常的类,

修改exception.java,添加(expectedExceptions = ArithmeticException.class):

import org.testng.annotations.Test;

public class exception {
@Test(expectedExceptions = ArithmeticException.class)
public void testMethod() {
int a = 0;
int b = 10;
System.out.println(b / a);
}
}

再次运行,结果通过:

PASSED: testMethod

===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

如果是一个正常的代码,期望抛出异常的话,会怎样呢?

修改exception.java,不会抛出异常:

import org.testng.annotations.Test;

public class exception {
@Test(expectedExceptions = ArithmeticException.class)
public void testMethod() {
int a = 0;
int b = 10;
System.out.println(a / b);
}
}

再次运行,结果失败:

FAILED: testMethod
org.testng.TestException:
Expected exception java.lang.ArithmeticException but got org.testng.TestException:
Method exception.testMethod() should have thrown an exception of class java.lang.ArithmeticException
at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1416)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1184)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1147)
at org.testng.TestRunner.privateRun(TestRunner.java:749)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:317)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274)
at org.testng.SuiteRunner.run(SuiteRunner.java:223)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1039)
at org.testng.TestNG.runSuitesLocally(TestNG.java:964)
at org.testng.TestNG.run(TestNG.java:900)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)
Caused by: org.testng.TestException:
Method exception.testMethod() should have thrown an exception of class java.lang.ArithmeticException
at org.testng.internal.Invoker.handleInvocationResults(Invoker.java:1442)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:722)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
... 17 more ===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================

testNG之异常测试的更多相关文章

  1. TestNg 6.异常测试

    * 什么时候会用到异常测试??* 在我们期望结果为某一个异常的时候* 比如:我们传入了某些不合法的参数,程序抛出异常* 也就是我的预期结果就是这个异常看以下的一段代码: package com.cou ...

  2. testng入门教程8 TestNG异常测试

    TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...

  3. TestNG异常测试

    用@Test(expectedExceptions = xxx) 声明 package com.janson; import org.testng.annotations.Test; public c ...

  4. TestNG(九) 异常测试

    package com.course.testng.suite; import org.testng.annotations.Test; public class ExpectedExeption { ...

  5. 廖雪峰Java8JUnit单元测试-2使用JUnit-2异常测试

    1.异常测试 对可能抛出的异常进行测试: 异常本身是方法签名的一部分: * public static int parseInt(String s) throws NumberFormatExcept ...

  6. 学习使用TestNG进行数据驱动测试

    转自: https://mp.weixin.qq.com/s/8Bd8LEhiC2pu2VMcyNMGlQ 学习使用TestNG进行数据驱动测试 赵吃饭 51Testing软件测试网 前天   学习使 ...

  7. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  8. testng多线程并行执行测试

    testng多线程并行执行测试 testng多线程并行执行测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者子组件的能力.TestNG允许我们以 ...

  9. testNG 预期异常、忽略测试、超时测试

    通过@Test 注解的参数值实现如下的几种测试 一.通过 @Test(expectedExceptions=异常类名) 参数实现到达 预期指定的异常效果 @Test(expectedException ...

随机推荐

  1. StrictMode 严格模式

    StrictMode: 帮助程序员避免在主线程上执行耗时操作: 启动级别: 1. 启动线程级别:   setThreadPolicy() 2. 启动应用级别 :  setVmPolicy() —— 对 ...

  2. python3下tomorow模块报语法错误def async(n, base_type, timeout=None): ^ SyntaxError: invalid syntax

    python3 pip 安装tomorrow模块,调用时候会报错:def async(n, base_type, timeout=None): ^ SyntaxError: invalid synta ...

  3. windows平台使用MongoDB shell 来连接 MongoDB 服务器并创建数据库

    windows平台使用MongoDB shell 来连接 MongoDB 服务器并创建数据库 命令行进入MongoDB的bin目录运行mongod.exe mongod --dbpath c:\dat ...

  4. [NOIP模拟20]题解

    来自达哥的问候…… A.周 究级难题,完全不可做QAQ #include<cstdio> #include<iostream> #include<cstring> ...

  5. [CSP-S模拟测试]:春思(数学)

    蝶恋花·春景花褪残红青杏小.燕子飞时,绿水人家绕.枝上柳绵吹又少.天涯何处无芳草!墙里秋千墙外道.墙外行人,墙里佳人笑.笑渐不闻声渐悄.多情却被无情恼.(本词是伤春之作,写春景清新秀丽.同时,景中又有 ...

  6. Scrapy模拟登陆豆瓣抓取数据

    scrapy  startproject douban 其中douban是我们的项目名称 2创建爬虫文件 进入到douban 然后创建爬虫文件 scrapy genspider dou douban. ...

  7. Cent OS (二)常用的命令介绍

    1. 常用的Linux命令   序号 命令 对应英文 作用 01 ls list 查看当前文件夹下的内容 02 pwd print work directory 查看当前所在的文件夹 03 cd [目 ...

  8. appium定位学习

    前面也介绍过appium的一些定位方法,今天看到一篇博客,里面的方法总结的,就转载过来. 本文转自:https://www.cnblogs.com/Mushishi_xu/p/7685966.html ...

  9. 用js实现摇一摇功能

    function init(){ if (window.DeviceMotionEvent) { // 移动浏览器支持运动传感事件 window.addEventListener('devicemot ...

  10. 哪位有方法把 dd/mm/yyyy的字符串 格式化成yyyy-mm-dd

     哪位有方法把  dd/mm/yyyy的字符串 格式化成yyyy-mm-dd[总监]Dawood(656317124)  10:00:42啊,找到方法了.procedure TForm1.Button ...