转载自: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. 王者荣耀交流协会--第2次Scrum会议

    Scrum master:袁玥 要求1:工作照片 要求2:时间跨度:2017年10月14号  6:02--6:43  共计41min 要求3:地点:一食堂二楼两张桌子旁(靠近卖方便面那边) 要求4:立 ...

  2. 【状压dp】AC Challenge

    https://nanti.jisuanke.com/t/30994 把每道题的前置条件用二进制压缩,然后dp枚举所有可能状态,再枚举该状态是从哪一个节点转移来的,符合前置条件则更新. 代码: #in ...

  3. 设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接

    标题:设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接 里面有使用C#使用SqlServer的例子.

  4. ant build.xml 解释!

    Ant的概念  Make命令是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨 ...

  5. Kafka集群无法外网访问问题解决攻略

    Kafka无法集群外网访问问题解决方法  讲解本地消费者和生产者无法使用远程Kafka服务器的处理办法 服务搭建好Kafka服务后,机本.测试 OK,外面机器却无法访问,很是怪异. 环境说明:  Ka ...

  6. javascript 排序

    // 插入排序 将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列. 从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置.(如果待插入的元素与有序 ...

  7. hdu-题目:1058 Humble Numbes

    http://acm.hdu.edu.cn/showproblem.php?pid=1058 Problem Description A number whose only prime factors ...

  8. 【数据库】SQL两表之间:根据一个表的字段更新另一个表的字段

    1. 写法轻松,更新效率高:update table1 set field1=table2.field1,field2=table2.field2from table2where table1.id= ...

  9. elsarticle模板 去掉Preprint submitted to

    参考:http://latex.org/forum/viewtopic.php?t=11123 修改elsarticle.cls文件. 我的CTeX装在c盘中,elsarticle.cls文件路径为: ...

  10. 元素定位:selenium消息框处理 (alert、confirm、prompt)

    基础普及 alert对话框 .细分三种,Alert,prompt,confirm 1. alert() 弹出个提示框 (确定) 警告消息框 alert 方法有一个参数,即希望对用户显示的文本字符串.该 ...