用@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

TestNG异常测试的更多相关文章

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

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

  2. TestNg 6.异常测试

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

  3. TestNG(九) 异常测试

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

  4. testNG之异常测试

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

  5. Java自动化测试框架-10 - TestNG之测试结果篇

    1.-测试结果 1.1-成功,失败和断言 测试被认为是成功的,如果它不引发任何异常完成,还是它扔的预期异常(请参阅文档expectedExceptions属性上找到的@Test注释). 您的测试方法通 ...

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

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

  7. TestNG之测试执行后没有生成默认测试报告(IDEA)

    使用IDEA+TestNG进行测试,没有生成 测试报告,是因为没有勾选监听器使用默认报告,具体操作如下: “Run” -> "Edit Configurations" -&g ...

  8. TestNG 搭建测试框架 自动化测试

    框架层级及基本组件:    参考:https://www.cnblogs.com/jier888/p/8998724.html Java作为开发语言 Maven管理项目及Jar包 Testng作为测试 ...

  9. testng入门教程10 TestNG参数化测试

    在TestNG的另一个有趣的功能是参数测试.在大多数情况下,你会遇到这样一个场景,业务逻辑需要一个巨大的不同数量的测试.参数测试,允许开发人员运行同样的测试,一遍又一遍使用不同的值. TestNG让你 ...

随机推荐

  1. [Codeforces 1037E] Trip

    [题目链接] http://codeforces.com/problemset/problem/1037/E [算法] 首先离线 , 将问题倒过来考虑 , 转化为 : 每次删除一条边 , 此时最多有多 ...

  2. python 之filter()函数

    filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filt ...

  3. nodejs实现验证码

    http://www.9958.pw/post/nodejs_lesson http://www.9958.pw/post/nodejscapp

  4. MySQL基础 — 常用命令

    一.连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MySQ: 首先在打开cmd窗口,输入mysql -uroot -p ,然后空格进入MySQL控制台, ...

  5. 【js】JavaScript parser实现浅析

    最近笔者的团队迁移了webpack2,在迁移过程中,笔者发现webpack2中有相当多的兼容代码,虽然外界有很多声音一直在质疑作者为什么要破坏性更新,其实大家也都知道webpack1那种过于“灵活”的 ...

  6. C++函数重载的4种错误示例

    函数重载的4种错误示例: #include <iostream> #include <string> using namespace std; //函数重载 同函数名,函数重载 ...

  7. [转]C语言常见错误总结1

    指针与数组的对比c程序中,指针和数组在不少地方可以相互替换着用,让人产生一种错觉,以为两者是等价的 数组要么在静态存储区被创建(如全局数组),要么在栈上被创建.数组名对应着(而不是指向)一块内存,其地 ...

  8. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph

    In this problem, we will define a graph called star graph, and the question is to find the minimum d ...

  9. 2017杭电多校第六场1008 Kirinriki

    传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  10. DHTML_____window对象属性

    <html> <head> <meta charset="utf-8"> <title>window对象属性</title&g ...