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

创建一个类

  • 创建一个Java类进行测试说MessageUtil.java 在 C:\ > TestNG_WORKSPACE

  • 在printMessage()方法里添加一个错误条件

/*
* This class prints the given message on console.
*/
public class MessageUtil { private String message; //Constructor
//@param message to be printed
public MessageUtil(String message){
this.message = message;
} // prints the message
public void printMessage(){
System.out.println(message);
int a =0;
int b = 1/a;
} // add "Hi!" to the message
public String salutationMessage(){
message = "Hi!" + message;
System.out.println(message);
return message;
}
}

创建测试案例类

  • 创建一个Java测试类为 ExpectedExceptionTest.java。

  • 添加的ArithmeticException 和 testPrintMessage()测试用例的预期异常。

创建一个Java类文件名ExpectedExceptionTest.java 在 C:\ > TestNG_WORKSPACE

import org.testng.Assert;
import org.testng.annotations.Test; public class ExpectedExceptionTest {
String message = "Manisha";
MessageUtil messageUtil = new MessageUtil(message); @Test(expectedExceptions = ArithmeticException.class)
public void testPrintMessage() {
System.out.println("Inside testPrintMessage()");
messageUtil.printMessage();
}
@Test
public void testSalutationMessage() {
System.out.println("Inside testSalutationMessage()");
message = "Hi!" + "Manisha";
Assert.assertEquals(message,messageUtil.salutationMessage());
}
}

创建测试运行

创建 testng.xml 在 C:\ > TestNG_WORKSPACE 执行测试案例。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<classes>
<class name="ExpectedExceptionTest" />
</classes>
</test>
</suite>

编译MessageUtil 测试用例类使用javac

C:\TestNG_WORKSPACE>javac MessageUtil.java TestJunit.java

现在,运行测试运行,这将运行提供的测试用例类中定义的测试用例。

C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

验证输出。testPrintMessage()测试的情况下会获得通过。

Inside testPrintMessage()
Manisha
Inside testSalutationMessage()
Hi!Manisha ===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
=============================================== 文章转载自:易百教程 [http://www.yiibai.com]
本文标题:TestNG异常测试
转载请保留原文链接:http://www.yiibai.com/html/testng/2013/0915301.html

testng入门教程8 TestNG异常测试的更多相关文章

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

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

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

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

  3. testng入门教程9 TestNG依赖测试

    有时候,你可能需要在一个特定的顺序调用方法在测试案例,或你想分享一些数据和方法之间的状态.TestNG支持这种依赖测试方法之间的显式依赖它支持声明. TestNG允许指定依赖,无论与否: 使用属性de ...

  4. testng入门教程7 TestNG组测试

    在TestNG中组测试是一个新的创新功能,它不存在于JUnit框架,它允许调度到适当的部分方法和瓶坯复杂的测试方法分组.您不仅可以声明属于群体的那些方法,但你也可以指定一组包含其他组.然后,TestN ...

  5. testng入门教程6 TestNG忽略测试

    有时,我们的代码是没有准备好,如果测试用例写入到测试方法/代码将无法运行,在这种情况下,@Test(enabled = false)有助于禁用此测试案例. 测试方法是标注了@Test(enabled ...

  6. testng入门教程11 TestNG运行JUnit测试

    现在,您已经了解了TestNG和它的各种测试,如果现在担心如何重构现有的JUnit代码,那就没有必要,使用TestNG提供了一种方法,从JUnit和TestNG按照自己的节奏.也可以使用TestNG执 ...

  7. TestNG 入门教程【转】

    TestNG 入门教程[转] 国庆7天假期,大部分朋友都出去旅游了,微信圈里全是晒旅游的照片, 东南亚游,欧洲游呀,真是羡慕呀. 悲惨的我只去了上海野生动物园, 在家休息,利用这段假期,把之前学过的东 ...

  8. testng入门教程16数据驱动(把数据写在xml)

    testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...

  9. testng入门教程5TestNG套件测试

    TestNG套件测试 测试套件的测试是为了测试软件程序的行为或一系列行为的情况下,是一个集合.在TestNG,我们不能定义一套测试源代码,但它代表的套件是一个XML文件执行特征.这也允许灵活的配置要运 ...

随机推荐

  1. Python 2.7.6 安装lxml模块[ubuntu14.04 LTS]

    lxml --->首字母是字母l,不是数字1 lxml 2.x : https://pypi.python.org/pypi/lxml/2.3 1xml官网:http://lxml.de/ 一 ...

  2. Node.j中path模块对路径的操作

    一.path模块 https://nodejs.org/docs/latest/api/path.html#path_path_join_paths 1.join方法 ==> 该方法将多个参数值 ...

  3. [工具] TreeSizeFree 查看每个文件夹的大小

    下载 URL :http://www.jam-software.com/treesize_free/ TreeSize Free这个软件可以非常简单方便的查看到每个文件夹的大小,而目录树的查看方式则让 ...

  4. Laravel 查询包括软删除的记录

    查询结果包括已被软删除的记录: Model::withTrashed()->get(); 只查询软删除记录: Model::onlyTrashed()->get(); PS:个人博客-La ...

  5. ubuntu部署nginx

    先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade再判断系统是否内置了add-apt-repository命令,如果没有执行下列命令安装 sudo ...

  6. New text file line delimiter

    Window -> Preferences -> General -> Workspace : Text file encoding :Default : 选择此项将设定文件为系统默 ...

  7. HOJ-2056 Bookshelf(线性动态规划)

    L is a rather sluttish guy. He almost never clean up his surroundings or regulate his personal goods ...

  8. Scala学习笔记(3)-表达式归纳

    语法:使用表达式定义值和变量 val <identifier>[:<type>]=<expression>  字面值类型 var <identifier> ...

  9. Django ---- 基础题

    一. 1.写出你所知道Djang有关的所有命令(下载.安装等) .安装django pip install django ==  .创建项目 diango­admin startproject 项目名 ...

  10. 2018-2019-2 20165330《网络对抗技术》Exp4 恶意代码分析

    目录 基础问题 相关知识 实验目的 实验内容 实验步骤 实验过程中遇到的问题 实验总结与体会 实验目的 监控你自己系统的运行状态,看有没有可疑的程序在运行 分析一个恶意软件,就分析Exp2或Exp3中 ...