TestNG
经常在使用TestNG的注解时,忘记这些差异不大的注解的执行顺序;或者忘记用什么注解。所以这里归纳总结一下。
注解 说明
@BeforeSuite The annotated method will be run before all tests in this suite have run.
被注释的方法只运行一次,在整个测试集之前运行
@AfterSuite The annotated method will be run after all tests in this suite have run.
被注释的方法只运行一次,在整个测试集之后运行
@BeforeTest The annotated method will be run before any test method belonging to the classes inside the
被注释的方法在任一一个,属于有
@AfterTest The annotated method will be run after all the test methods belonging to the classes inside the
被注释的方法在所有,属于有
@BeforeClass The annotated method will be run before the first test method in the current class is invoked.
被注释的方法将在当前类的第一个测试方法调用前运行
@AfterClass The annotated method will be run after all the test methods in the current class have been run.
被注释的方法将在当前类的所有测试方法调用后运行
@BeforeGroups The 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前运行。这个方法保证在第一个属于这些组的测试方法调用前立即执行。
@AfterGroups The 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后运行。这个方法保证在最后一个属于这些组的测试方法调用后立即执行。
@BeforeMethod The annotated method will be run before each test method.
被注释的方法将在每一个测试方法调用前运行。
@AfterMethod The 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提供的一些特性
- before方法和after方法,带来了足够丰富的测试生命周期控制
- dependsOnGroups/dependsOnMethods 提供了依赖检查机制,并可以严格控制执行顺序
- DataProvider 使得对同一个方法的测试覆盖变的非常轻松,非常适合进行边界测试,只要给出多种测试数据就可以针对一个测试方法进行覆盖
- expectedExceptions 使得异常测试变的非常轻松
- invocationCount/threadPoolSize 终于可以简单的直接进行多线程测试了,这个绝对是junit的超级弱项,回想junit中那个万恶的System.exist(0)…
- timeOut 终于不用死等然后手工强行关闭测试
TestNG官方文档中文版(3)-testng.xml ,原文请见 http://testng.org/doc/documentation-main.html
参考文章:http://topmanopensource.iteye.com/blog/1983735
-——————————
作者:aduocd
https://blog.csdn.net/aduocd/article/details/52188977
还没有评论,来说两句吧...