@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. 浅谈js for循环输出i为同一值的问题(闭包解决)

    1.最近开发中遇到一个问题,为什么每次输出都是5,而不是点击每个p,就alert出对应的1,2,3,4,5. <html> <head> <meta http-equiv ...

  2. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  3. cf 1263

    A #include<bits/stdc++.h> using namespace std; int main(){ int t;cin>>t; while(t--){ ]; ...

  4. 10.18.1 linux文本编辑器vim

    vi和vim的区别 编辑一个文本时,vi不会显示颜色,而vim会显示颜色,vi 有点类似windows记事本,简单,那么就是vim复杂编辑器,功能复杂,高亮,自动缩进(写shell/python脚本用 ...

  5. paper 154:姿态估计(Hand Pose Estimation)相关总结

    Awesome Works  !!!! Table of Contents Conference Papers 2017 ICCV 2017 CVPR 2017 Others 2016 ECCV 20 ...

  6. python数据储存

    python数据储存 csv文件的操作 安装csv包打开cmd 执行 pip install csv引入的模块名为csv 读取文件 with open("xx.csv"," ...

  7. 【原】webpack--plugins,主要解释plugins干了啥

    其实呢,plugins是增强webpack的功能, 插件用于bundle文件的优化,资源管理和环境变量的注入, 可以理解为任何loaders不能做的事让它来做, 作用于整个构建过程. 常见的plugi ...

  8. 单例模式@Singleton在测试中的运用

    前言 单例模式是一种比较常用的设计模式,目的是:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 测试种可能用到的场景 : 在很多时候,有些对象我们希望在整个程序只有一个实例,如线程池.数据库连 ...

  9. C语言|博客作业3

    问题 答案 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://i.cnblogs.com/EditPosts.aspx?postid=11661995&update ...

  10. BZOJ 2122 [分块+单调栈+二分](有详解)

    题面 传送门 给定序列d和lim.假设有一个初始价值\(x_0\),则经历第i天后价值变为\(min(x_0+d[i],lim[i])\),记\(f(i,j,x_0)\)表示以初始代价x0依次经过第i ...