TestNG中的注解说明

目的:罗列TestNG中的常用注解

经常在使用TestNG的注解时,忘记这些差异不大的注解的执行顺序;或者忘记用什么注解。所以这里归纳总结一下。

注解说明
@BeforeSuiteThe annotated method will be run before all tests in this suite have run.

被注释的方法只运行一次,在整个测试集之前运行
@AfterSuiteThe annotated method will be run after all tests in this suite have run.

被注释的方法只运行一次,在整个测试集之后运行
@BeforeTestThe annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

被注释的方法在任一一个,属于有<test>标签的类的测试方法之前运行
@AfterTestThe annotated method will be run after all the test methods belonging to the classes inside the <test> tag have run.

被注释的方法在所有,属于有<test>标签的类的测试方法之后运行
@BeforeClassThe annotated method will be run before the first test method in the current class is invoked.

被注释的方法将在当前类的第一个测试方法调用前运行
@AfterClassThe annotated method will be run after all the test methods in the current class have been run.

被注释的方法将在当前类的所有测试方法调用后运行
@BeforeGroupsThe list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

被注释的方法将在列表中的gourp前运行。这个方法保证在第一个属于这些组的测试方法调用前立即执行。
@AfterGroupsThe list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

被注释的方法将在列表中的gourp后运行。这个方法保证在最后一个属于这些组的测试方法调用后立即执行。
@BeforeMethodThe annotated method will be run before each test method.

被注释的方法将在每一个测试方法调用前运行。
@AfterMethodThe annotated method will be run after each test method.

被注释的方法将在每一个测试方法调用后运行。
@DataProvider标记一个方法用于为测试方法提供数据。被注释的方法必须返回Object[][], 其中每个Object[]可以指派为这个测试方法的参数列表。从这个DataProvider接收数据@Test方法需要使用一个和当前注释相同名称的dataProvider名称。其中name-- 这个DataProvider的名称
@Factory标记方法作为一个返回对象的工厂,这些对象将被TestNG用于作为测试类。这个方法必须返回Object[]
@Parameters描述如何传递参数给@Test方法。其中value--用于填充这个方法的参数的变量列表
@Test标记一个类或方法作为测试的一部分


属性说明
alwaysRun对于每个bufore方法(beforeSuite, beforeTest, beforeTestClass 和 beforeTestMethod, 但是不包括 beforeGroups):如果设置为true,被配置的方法将总是运行而不管它属于哪个组。
对于after方法(afterSuite, afterClass, ...): 如果设置为true,被配置的方法甚至在一个或多个先调用的方法失败或被忽略时也将运行。
dependsOnGroups这个方法依赖的组列表
dependsOnMethods这个方法依赖的方法列表
enabled这个类的方法是否激活
groups这个类或方法所属的分组列表
inheritGroups如果设置为true,这个方法被属于在类级别被@Test annotation指定的组
alwaysRun如果设置为true,这个测试方法将总是运行,甚至当它依赖的方法失败时。
dataProvider这个测试方法的data provider的名称
dataProviderClass用于查找data provider的类。
如果不指定,将在当前测试方法所在的类或者它的基类上查找data provider。
如果这个属性被指定, 则data provider方法需要是指定类的static方法。
dependsOnGroups当前方法依赖的组列表
dependsOnMethods当前方法依赖的方法列表
description当前方法的描述
enabled当前类的方法/方法是否被激活
expectedExceptions测试方法期望抛出的异常列表。如果没有异常或者抛出的不是列表中的任何一个,当前方法都将标记为失败.
groups当前类/方法所属的组列表
invocationCount当前方法被调用的次数
successPercentage当前方法期望的成功率
sequential如果设置为true,当前测试类上的所有方法保证按照顺序运行。甚至测试们在parallel="true"的情况下.这个属性只能用于类级别,如果用于方法级别将被忽略。
timeOut当前方法容许花费的最大时间,单位毫秒。
threadPoolSize当前方法的线程池大小。方法将被多线程调用,次数由invocationCount参数指定。
注意:如果invocationCount没有指定则这个属性将被忽略


 上面是TestNG中用到的annotation列表,从中我们可以看到TestNG提供的一些特性

1. before方法和after方法,带来了足够丰富的测试生命周期控制
2. dependsOnGroups/dependsOnMethods 提供了依赖检查机制,并可以严格控制执行顺序
3. DataProvider 使得对同一个方法的测试覆盖变的非常轻松,非常适合进行边界测试,只要给出多种测试数据就可以针对一个测试方法进行覆盖
4. expectedExceptions 使得异常测试变的非常轻松
5. invocationCount/threadPoolSize 终于可以简单的直接进行多线程测试了,这个绝对是junit的超级弱项,回想junit中那个万恶的System.exist(0)...
6. timeOut 终于不用死等然后手工强行关闭测试

TestNG官方文档中文版(3)-testng.xml ,原文请见 http://testng.org/doc/documentation-main.html

参考文章:http://topmanopensource.iteye.com/blog/1983735


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值