转载自:http://blog.csdn.net/bigapplestar/article/details/7300137

今天突然收到通知,统一改用TestNG写测试用例,开始查这方面的资料,学习一下。

TestNG需要有自己的配置文件,最方便的办法即是从eclipse上直接下载一下插件。直接下载跟插件下载的地址都可以在http://testng.org/doc/download.html上找到。

http://www.mkyong.com/tutorials/testng-tutorials/ 这个教程写得很好,挺通俗易懂的。

大致摘抄记录一下:

1. 基本用法。

[java] view
plain
copy

  1. import java.util.*;
  2. import org.testng.Assert;
  3. import org.testng.annotations.*;
  4. public class TestNGTest1 {
  5. private Collection collection;
  6. @BeforeClass
  7. public void oneTimeSetUp() {
  8. // one-time initialization code
  9. System.out.println("@BeforeClass - oneTimeSetUp");
  10. }
  11. @AfterClass
  12. public void oneTimeTearDown() {
  13. // one-time cleanup code
  14. System.out.println("@AfterClass - oneTimeTearDown");
  15. }
  16. @BeforeMethod
  17. public void setUp() {
  18. collection = new ArrayList();
  19. System.out.println("@BeforeMethod - setUp");
  20. }
  21. @AfterMethod
  22. public void tearDown() {
  23. collection.clear();
  24. System.out.println("@AfterMethod - tearDown");
  25. }
  26. @Test
  27. public void testEmptyCollection() {
  28. Assert.assertEquals(collection.isEmpty(),true);
  29. System.out.println("@Test - testEmptyCollection");
  30. }
  31. @Test
  32. public void testOneItemCollection() {
  33. collection.add("itemA");
  34. Assert.assertEquals(collection.size(),1);
  35. System.out.println("@Test - testOneItemCollection");
  36. }
  37. }

@BeforeClass @AfterClass 等这些从字面上都可以很好理解,Class是整个测试类运行时的前后,而Method则在每个测试方法被调用前都会被调用。

所以这一段代码执行后的结果如下:

[java] view
plain
copy

  1. @BeforeClass - oneTimeSetUp
  2. @BeforeMethod - setUp
  3. @Test - testEmptyCollection
  4. @AfterMethod - tearDown
  5. @BeforeMethod - setUp
  6. @Test - testOneItemCollection
  7. @AfterMethod - tearDown
  8. @AfterClass - oneTimeTearDown
  9. PASSED: testEmptyCollection
  10. PASSED: testOneItemCollection

2.测试预期的异常

可以检测某一方法检测到某一异常时是否能按预期地抛出。

[java] view
plain
copy

  1. import org.testng.annotations.*;
  2. /**
  3. * TestNG Expected Exception Test
  4. * @author mkyong
  5. *
  6. */
  7. public class TestNGTest2 {
  8. @Test(expectedExceptions = ArithmeticException.class)
  9. public void divisionWithException() {
  10. int i = 1/0;
  11. }
  12. }

在这一示例中,divisionWithException()将会抛出一个ArithmetricException的预期异常,这个单元测试也将顺利通过。

3. 忽略某一测试方法

TestNG是通过直接在方法上加标注的方式来进行测试,而这里也可以设置某个测试方法不工作。可以通过如下方式:

[java] view
plain
copy

  1. import org.testng.annotations.*;
  2. /**
  3. * TestNG Ignore Test
  4. * @author mkyong
  5. *
  6. */
  7. public class TestNGTest3 {
  8. @Test(enabled=false)
  9. public void divisionWithException() {
  10. System.out.println("Method is not ready yet");
  11. }
  12. }

4. 时限测试

可以设置一个特定时长的限制(以毫秒ms为单位),一旦测试的内容运行超过了该 时间长度,那么将会终止,同时标记为failed。

[java] view
plain
copy

  1. import org.testng.annotations.*;
  2. /**
  3. * TestNG TimeOut Test
  4. * @author mkyong
  5. *
  6. */
  7. public class TestNGTest4 {
  8. @Test(timeOut = 1000)
  9. public void infinity() {
  10. while (true);
  11. }
  12. }

运行后将会有如下的提示:

[java] view
plain
copy

  1. FAILED: infinity
  2. org.testng.internal.thread.ThreadTimeoutException:
  3. Method public void TestNGTest4.infinity() didn't finish within the time-out 1000
  4. ... Removed 18 stack frames

5. 测试套件(Suite Test)

即是将一些单元测试用例绑定并一起运行。定义suite test是在xml文件中,参见如下文件,表示将TestNG1和TestNG2一起执行。

[html] view
plain
copy

  1. <!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
  2. <suite name="My test suite">
  3. <test name="testing">
  4. <classes>
  5. <class name="TestNG1" />
  6. <class name="TestNG2" />
  7. </classes>
  8. </test>
  9. </suite>

6. 依赖测试(Dependency Test)

这也可以用于解决如何决定一个测试类中各测试方法调用顺序的问题,可以指定某一个方法依赖于另一个方法的预先执行。

[java] view
plain
copy

  1. import org.testng.annotations.*;
  2. /**
  3. * TestNG Dependency Test
  4. * @author mkyong
  5. *
  6. */
  7. public class TestNGTest7 {
  8. @Test
  9. public void method1() {
  10. System.out.println("This is method 1");
  11. }
  12. @Test(dependsOnMethods={"method1"})
  13. public void method2() {
  14. System.out.println("This is method 2");
  15. }
  16. }

运行的结果为:

[java] view
plain
copy

  1. PASSED: method1
  2. PASSED: method2

method仅当在method1方法执行成功的前提下才会运行。

这里翻译得顺序有些调整,关于参数传递的6,7小节下次再翻译,还没有具体测试过,同时也还有一些疑问,若传递的是数据,类等,试试再来整理出来了。

TestNG指南的更多相关文章

  1. ☕【Java技术指南】「TestNG专题」单元测试框架之TestNG使用教程指南(上)

    TestNG介绍 TestNG是Java中的一个测试框架, 类似于JUnit 和NUnit, 功能都差不多, 只是功能更加强大,使用也更方便. 详细使用说明请参考官方链接:https://testng ...

  2. Spring、Spring Boot和TestNG测试指南 - 使用Spring Boot Testing工具

    Github地址 前面一个部分讲解了如何使用Spring Testing工具来测试Spring项目,现在我们讲解如何使用Spring Boot Testing工具来测试Spring Boot项目. 在 ...

  3. [翻译]现代java开发指南 第二部分

    现代java开发指南 第二部分 第二部分:部署.监控 & 管理,性能分析和基准测试 第一部分,第二部分 =================== 欢迎来到现代 Java 开发指南第二部分.在第一 ...

  4. JUnit5 快速指南

    JUnit5 快速指南 version: junit5 1. 安装 2. JUnit 注解 3. 编写单元测试 3.1. 基本的单元测试类和方法 3.2. 定制测试类和方法的显示名称 3.3. 断言( ...

  5. Self20171218_Eclipse+TestNg HelloWorld

    作为一个经典的入门例子,这里展示如何开始使用TestNG单元测试框架. 使用的工具 : TestNG 6.8.7 Maven 3 Eclipse IDE TestNG下载并安装 从这里 http:// ...

  6. 《gradle 用户指南中文版》目录

    gradle 用户指南 版权所有©2007-2017 Hans Dockter,Adam Murdoch只要您不对这些副本收取任何费用,并且进一步规定,每个副本都包含本版权声明,无论是以印刷版还是电子 ...

  7. TestNG Hello World入门示例

    https://www.yiibai.com/testng/hello-world-example.html https://www.yiibai.com/testng/ 作为一个经典的入门例子,这里 ...

  8. maven相关说明,以及使用Testng相关

    配置Apache Maven Apache Maven使用本身的配置和建立的项目位于许多地方: MAVEN_OPTS环境变量: 该变量包含用于启动运行Maven的JVM的参数,可用于向Maven提供其 ...

  9. dubbo开发者指南

    开发者指南 参与 流程 任务 版本管理 源码构建 框架设计 整体设计 模块分包 依赖关系 调用链 暴露服务时序 引用服务时序 领域模型 基本原则 扩展点加载 扩展点配置 扩展点自动包装 扩展点自动装配 ...

随机推荐

  1. 02_Java基础_第2天(变量、运算符)_讲义

    今日内容介绍 1.变量 2.运算符 01变量概述 * A: 什么是变量? * a: 变量是一个内存中的小盒子(小容器),容器是什么?生活中也有很多容器, * 例如水杯是容器,用来装载水:你家里的大衣柜 ...

  2. 对IT行业的一些思考

          阅读完两篇报道,从“2014年十大最热门行业和职业排行榜”可以看出最热门的行业是IT行业,可以看出IT行业在未来的发展前景很乐观,选择IT行业的人也会越来越多,IT行业也会越来越庞大.但是 ...

  3. Markdown使用github风格时报TLS错误解决办法

    https://docs.microsoft.com/en-us/officeonlineserver/enable-tls-1-1-and-tls-1-2-support-in-office-onl ...

  4. 【week10】psp

    项目 内容 开始时间 结束时间 中断时间 净时间 2016/11/19(星期六) 写博客 吉林一日游规格说明书 10:30 15:10 20 260 2016/11/20(星期日) 看论文 磷酸化+三 ...

  5. 小程序 switch按钮

    <view class='pay-switch'> <switch color='#1F3238' data-gongprice='{{gongprice}}' data-disco ...

  6. 【C++】C++的构造函数

    构造函数是特殊的成员函数,只要创建类类型的对象,都要执行构造函数.构造函数的工作是保证每个对象的数据成员具有合适的初始值. class Sales_Item { public: //operation ...

  7. 洛谷P2894[USACO08FEB]酒店Hotel(线段树)

    问题描述 奶牛们最近的旅游计划,是到苏必利尔湖畔,享受那里的湖光山色,以及明媚的阳光.作为整个旅游的策划者和负责人,贝茜选择在湖边的一家著名的旅馆住宿.这个巨大的旅馆一共有N (1 <= N & ...

  8. CodeChef KnightMov

    码死了...考试的时候基本上是写一会儿思考一会儿人生....考完了调了调...最后400行+....不应该这么长的....以后重写一下再补题解..... 也许这就是蒟蒻吧.jpg 安利cstdio博客 ...

  9. 【bzoj3730】震波 动态点分治+线段树

    题目描述 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i].不幸的是,这片土地常常发生地震,并且随着时代的发展,城市 ...

  10. Python 3中的str和bytes类型

    Python3 中的str和bytes类型 Python3最重要的新特性之一是:对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Pyt ...