TestNG指南

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

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

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

    大致摘抄记录一下:

    1. 基本用法。

import java.util.*;
import org.testng.Assert;
import org.testng.annotations.*;
 
public class TestNGTest1 {
 
    private Collection collection;
 
    @BeforeClass
    public void oneTimeSetUp() {
        // one-time initialization code   
    	System.out.println("@BeforeClass - oneTimeSetUp");
    }
 
    @AfterClass
    public void oneTimeTearDown() {
        // one-time cleanup code
    	System.out.println("@AfterClass - oneTimeTearDown");
    }
 
    @BeforeMethod
    public void setUp() {
        collection = new ArrayList();
        System.out.println("@BeforeMethod - setUp");
    }
 
    @AfterMethod
    public void tearDown() {
        collection.clear();
        System.out.println("@AfterMethod - tearDown");
    }
 
    @Test
    public void testEmptyCollection() {
        Assert.assertEquals(collection.isEmpty(),true);
        System.out.println("@Test - testEmptyCollection");
    }
 
    @Test
    public void testOneItemCollection() {
        collection.add("itemA");
        Assert.assertEquals(collection.size(),1);
        System.out.println("@Test - testOneItemCollection");
    }
}

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

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


@BeforeClass - oneTimeSetUp
@BeforeMethod - setUp
@Test - testEmptyCollection
@AfterMethod - tearDown
@BeforeMethod - setUp
@Test - testOneItemCollection
@AfterMethod - tearDown
@AfterClass - oneTimeTearDown
PASSED: testEmptyCollection
PASSED: testOneItemCollection

    2.测试预期的异常

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

   

import org.testng.annotations.*;
 
/**
 * TestNG Expected Exception Test
 * @author mkyong
 *
 */
public class TestNGTest2 {
 
	@Test(expectedExceptions = ArithmeticException.class)  
	public void divisionWithException() {  
	  int i = 1/0;
	}  
 
}

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

    3. 忽略某一测试方法

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

import org.testng.annotations.*;
 
/**
 * TestNG Ignore Test
 * @author mkyong
 *
 */
public class TestNGTest3 {
 
	@Test(enabled=false)
	public void divisionWithException() {  
	  System.out.println("Method is not ready yet");
	}  
 
}

    4. 时限测试

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

   

import org.testng.annotations.*;
 
/**
 * TestNG TimeOut Test
 * @author mkyong
 *
 */
public class TestNGTest4 {
 
	@Test(timeOut = 1000)  
	public void infinity() {  
		while (true);  
	}  
 
}

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

   

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

    5. 测试套件(Suite Test)

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

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
<suite name="My test suite">
  <test name="testing">
    <classes>
       <class name="TestNG1" />
       <class name="TestNG2" />
    </classes>
  </test>
</suite>

   6. 依赖测试(Dependency Test)

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

   

import org.testng.annotations.*;
 
/**
 * TestNG Dependency Test
 * @author mkyong
 *
 */
public class TestNGTest7 {
 
	@Test
	public void method1() {
	   System.out.println("This is method 1");
	}
 
	@Test(dependsOnMethods={"method1"})
	public void method2() {
		System.out.println("This is method 2");
	}
 
 
}

    运行的结果为:

   

PASSED: method1
PASSED: method2

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



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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值