1. Before类别和After类别注解

  • @BeforeSuite
  • @AfterSuite
  • @BeforeTest
  • @AfterTest
  • @BeforeClass
  • @AfterClass
  • @BeforeMethod
  • @AfterMethod

上述的注解分为Before类别和After类,我们可以在Before类别的注解方法里面做一些初始化动作,如实例化数据库连接、新建数据库连接池、创建线程池、打开文件流等等。然后,我们可以在After类别的注解方法里面做一些销毁动作,如释放数据库连接、销毁数据库连接池、销毁线程池或者关闭文件流等等。同一类别的不同注解会在不同的位置被调用,下面我们逐个介绍:

1.1 @BeforeSuite

被@BeforeSuite注解的方法,将会在testng定义的xml根元素里面的所有执行之前运行。

1.2 @AfterSuite

被@AfterSuite注解的方法,将会在testng定义的xml根元素里面的所有执行之后运行。

1.3 @BeforeTest

被@BeforeTest注解的方法,将会在一个元素定义的所有里面所有测试方法执行之前运行。

1.4 @AfterTest

被@AfterTest注解的方法,将会在一个元素定义的所有里面所有的测试方法执行之后运行。

1.5 @BeforeClass

被@BeforeClass注解的方法,将会在当前测试类的第一个测试方法执行之前运行。

1.6 @AfterClass

被@AfterClass注解的方法,将会在当前测试类的最后一个测试方法执行之后运行。

1.7 @BeforeMethod

被@BeforeMethod注解的方法,将会在当前测试类的每一个测试方法执行之前运行。

1.8 @AfterMethod

被@AfterMethod注解的方法,将会在当前测试类的每一个测试方法执行之后运行。

上面的说明,很抽象,也很乱,我们以一个例子来说明上面这些注解的用法:

新建TestNGAnnotationTestTestNGAnnotationTest2(TestNGAnnotationTest2TestNGAnnotationTest内容一致)

public class TestNGAnnotationTest {

    @BeforeSuite
public void beforeSuite() {
System.out.println(this.getClass().getName() + " beforeSuite");
} @AfterSuite
public void afterSuite() {
System.out.println(this.getClass().getName() + " afterSuite");
} @BeforeTest
public void beforeTest() {
System.out.println(this.getClass().getName() + " beforeTest");
} @AfterTest
public void afterTest() {
System.out.println(this.getClass().getName() + " afterTest");
} @BeforeClass
public void beforeClass() {
System.out.println(this.getClass().getName() + " beforeClass");
} @AfterClass
public void afterClass() {
System.out.println(this.getClass().getName() + " afterClass");
} @BeforeMethod
public void beofreMethod() {
System.out.println(this.getClass().getName() + " beforeMethod");
} @AfterMethod
public void afterMethod() {
System.out.println(this.getClass().getName() + " afterMethod");
} @Test
public void test1() {
System.out.println(this.getClass().getName() + " test1");
} @Test
public void test2() {
System.out.println(this.getClass().getName() + " test2");
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

testng.xml里面加入:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="test1" >
<classes>
<class name="com.crazypig.testngdemo.TestNGAnnotationTest" />
<class name="com.crazypig.testngdemo.TestNGAnnotationTest2" />
</classes>
</test>
</suite>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

运行testng测试,得到以下结果:

com.crazypig.testngdemo.TestNGAnnotationTest beforeSuite
com.crazypig.testngdemo.TestNGAnnotationTest2 beforeSuite
com.crazypig.testngdemo.TestNGAnnotationTest beforeTest
com.crazypig.testngdemo.TestNGAnnotationTest2 beforeTest
com.crazypig.testngdemo.TestNGAnnotationTest beforeClass
com.crazypig.testngdemo.TestNGAnnotationTest beforeMethod
com.crazypig.testngdemo.TestNGAnnotationTest test1
com.crazypig.testngdemo.TestNGAnnotationTest afterMethod
com.crazypig.testngdemo.TestNGAnnotationTest beforeMethod
com.crazypig.testngdemo.TestNGAnnotationTest test2
com.crazypig.testngdemo.TestNGAnnotationTest afterMethod
com.crazypig.testngdemo.TestNGAnnotationTest afterClass
com.crazypig.testngdemo.TestNGAnnotationTest2 beforeClass
com.crazypig.testngdemo.TestNGAnnotationTest2 beforeMethod
com.crazypig.testngdemo.TestNGAnnotationTest2 test1
com.crazypig.testngdemo.TestNGAnnotationTest2 afterMethod
com.crazypig.testngdemo.TestNGAnnotationTest2 beforeMethod
com.crazypig.testngdemo.TestNGAnnotationTest2 test2
com.crazypig.testngdemo.TestNGAnnotationTest2 afterMethod
com.crazypig.testngdemo.TestNGAnnotationTest2 afterClass
com.crazypig.testngdemo.TestNGAnnotationTest afterTest
com.crazypig.testngdemo.TestNGAnnotationTest2 afterTest
com.crazypig.testngdemo.TestNGAnnotationTest afterSuite
com.crazypig.testngdemo.TestNGAnnotationTest2 afterSuite
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

这里给出一张图,方便更好地理解这些注解方法的执行位置: 

我们可以根据自身需求,选择特定的位置去执行一些初始化动作,以及一些销毁动作。假如你需要针对整个测试suite做初始化动作,那么应该选择在被@BeforeSuite注解的方法里面执行。如果需要针对一个<test>里面的所有测试类做初始化动作,那么可以选择在被@BeforeTest注解的方法里面执行。如果需要针对一个特定的测试类做初始化动作,那么应该选择在被@BeforeClass注解的方法里面执行。最后,假如你想在每一个测试方法执行前做初始化动作,那么应该选择@BeforeMethod。销毁的选择与初始化类似,这里不再赘述。

2. @Test 注解

@Test 注解是TestNG的核心注解,被打上该注解的方法,表示为一个测试方法,类比JUnit是一个道理(JUnit也是用了这个注解,在使用TestNG时候注意导包别导错)。

这个注解有多个配置属性,用法为:

@Test(param1 = ..., param2 = ...)
  • 1
  • 1

常见取值说明如下:

  • alwaysRun : 如果=true,表示即使该测试方法所依赖的前置测试有失败的情况,也要执行
  • dataProvider : 选定传入参数的构造器。(@DataProvider注解将在后面章节介绍)
  • dataProviderClass : 确定参数构造器的Class类。(参数构造器首先会在当前测试类里面查找,如果参数构造器不在当前测试类定义,那么必须使用该属性来执行它所在的Class类)
  • dependsOnGroups : 确定依赖的前置测试组别。
  • dependsOnMethods : 确定依赖的前置测试方法。
  • description : 测试方法描述信息。(建议为每个测试方法添加有意义的描述信息,这将会在最后的报告中展示出来)
  • enabled : 默认为true,如果指定为false,表示不执行该测试方法。
  • expectedExceptions : 指定期待测试方法抛出的异常,多个异常以逗号(,)隔开。
  • groups : 指定该测试方法所属的组,可以指定多个组,以逗号隔开。组测试的用法将在后面文章单独介绍。
  • invocationCount : 指定测试方法需要被调用的次数。
  • invocationTimeOut: 每一次调用的超时时间,如果invocationCount没有指定,该参数会被忽略。应用场景可以为测试获取数据库连接,超时就认定为失败。单位是毫秒。
  • priority : 指定测试方法的优先级,数值越低,优先级越高,将会优先与其他数值高的测试方法被调用。(注意是针对一个测试类的优先级)
  • timeout : 指定整个测试方法的超时时间。单位是毫秒。

下面我们写一个简单的测试类,说明@Test注解的使用以及属性的配置方式:

package com.crazypig.testngdemo;

import org.testng.annotations.Test;

public class TestAnnotationPropertiesTest {

    @Test(priority = 1, invocationCount = 3)
public void test1() {
System.out.println("invoke test1");
} @Test(priority = 2, invocationCount = 2)
public void test2() {
System.out.println("invoke test2");
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

testng.xml配置

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1" >
<test name="test1" >
<classes>
<class name="com.crazypig.testngdemo.TestAnnotationPropertiesTest" />
</classes>
</test>
</suite>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行结果:

invoke test1
invoke test1
invoke test1
invoke test2
invoke test2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

3. @Parameters 注解

@Parameters 注解用于为测试方法传递参数, 用法如下所示:

package com.crazypig.testngdemo;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test; public class AnnotationParametersTest { @Parameters(value = {"param1", "param2"})
@Test
public void test(String arg1, String arg2) {
System.out.println("use @Parameters to fill method arguments : arg 1 = " + arg1 + ", arg2 = " + arg2);
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

testng.xml配置

<test name="testAnnotationParameters">
<parameter name="param1" value="value1"></parameter>
<parameter name="param2" value="value2"></parameter>
<classes>
<class name="com.crazypig.testngdemo.AnnotationParametersTest" />
</classes>
</test>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

运行结果:

use @Parameters to fill method arguments : arg 1 = value1, arg2 = value2
  • 1
  • 1

4. @DataProvider 注解

上面的小结提到@Parameters注解可以为测试方法传递参数,但是这种方式参数值需要配置在testng.xml里面,灵活性不高。而@DataProvider注解同样可以为测试方法传递参数值,并且,它是真正意义上的参数构造器,可以传入多组测试数据对测试方法进行测试。被@DataProvider注解的方法,方法返回值必须为Object[][]或者Iterator<Object[]>。例子如下所示:

package com.crazypig.testngdemo;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; public class AnnotationDataProviderTest { @DataProvider(name="testMethodDataProvider")
public Object[][] testMethodDataProvider() { return new Object[][]{{"value1-1", "value2-1"}, {"value1-2", "value2-2"}, {"value1-3", "value2-3"}}; } @Test(dataProvider="testMethodDataProvider")
public void test(String arg1, String arg2) {
System.out.println("use @DataProvider to fill method argument : arg1 = " + arg1 + " , arg2 = " + arg2);
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

testng.xml配置:

<test name="testDataProvider">
<classes>
<class name="com.crazypig.testngdemo.AnnotationDataProviderTest" />
</classes>
</test>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

运行结果:

use @DataProvider to fill method argument : arg1 = value1-1 , arg2 = value2-1
use @DataProvider to fill method argument : arg1 = value1-2 , arg2 = value2-2
use @DataProvider to fill method argument : arg1 = value1-3 , arg2 = value2-3
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

5. @Factory 注解

在一个方法上面打上@Factory注解,表示该方法将返回能够被TestNG测试的测试类。利用了设计模式中的工厂模式。例子如下所示:

package com.crazypig.testngdemo;

import org.testng.annotations.Factory;

public class AnnotationFactoryTest {

    @Factory
public Object[] getSimpleTest() {
return new Object[]{ new SimpleTest("one"), new SimpleTest("two")};
} }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
package com.crazypig.testngdemo;

import org.testng.annotations.Test;

public class SimpleTest {

    private String param;

    public SimpleTest(String param) {
this.param = param;
} @Test
public void test() {
System.out.println("SimpleTest.param = " + param);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

testng.xml配置:

<test name="testFactory">
<classes>
<class name="com.crazypig.testngdemo.AnnotationFactoryTest" />
</classes>
</test>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

运行结果:

SimpleTest.param = one
SimpleTest.param = two
  • 1
  • 2
  • 1
  • 2

6. @Listeners 注解

一般我们写测试类不会涉及到这种类型的注解,这个注解必须定义在类、接口或者枚举类级别。实用的Listener包括ISuiteListenerITestListenerIInvokedMethodListener,他们可以在suite级别、test级别和test method一些执行点执行一些自定义操作,如打印日志。因为很少使用,这里不以例子形式给出,感兴趣的可以自己研究一下。

TestNG基本注解(二)的更多相关文章

  1. 【转】TestNG常用注解

    http://blog.csdn.net/d6619309/article/details/52435084 TestNG的注解大部分用在方法级别上.常用的注解列举如下: 1. Before类别和Af ...

  2. TestNG基本注解

    TestNG的注解: 注解 描述 @BeforeSuite 注解的方法将只运行一次,运行所有测试前此套件中. @AfterSuite 注解的方法将只运行一次此套件中的所有测试都运行之后. @Befor ...

  3. TestNG基本注解(一)

    TestNG基本注解   注解 描述 @BeforeSuite 注解的方法将只运行一次,运行所有测试前此套件中. @AfterSuite 注解的方法将只运行一次此套件中的所有测试都运行之后. @Bef ...

  4. TestNG系列(二)TestNG注解

    前言 TetsNG提供了很多注解,允许测试人员灵活地组织测试用例 一.@Test @Tets是TestNG的核心注解,被注解的方法,表示为一个测试方法. description属性 @Test(des ...

  5. TestNG入门——注解之Before/After

    注解是java 5新增的功能,可使用于类,方法,变量,testNG包提供的注解功能请见下表 1.@BeforeSuite or @AfterSuite  被注解的方法,将在整个测试套件之前 or 之后 ...

  6. TestNG之注解的生命周期

    有必要介绍一下TestNG注解的生命周期,先看一下官网支持的注解有 @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeGroups@AfterGro ...

  7. TestNG基本注解(注释)

    传统的方式来表示JUnit3中的测试方法是测试自己的名字前缀.标记一个类中的某些方法,具有特殊的意义,这是一个非常有效的方法,但命名不很好的扩展(如果我们想添加更多标签为不同的框架?),而非缺乏灵活性 ...

  8. TestNG配置注解

    以下是TestNG支持的注释列表: 注解 描述 @BeforeSuite 在该套件的所有测试都运行在注释的方法之前,仅运行一次. @AfterSuite 在该套件的所有测试都运行在注释方法之后,仅运行 ...

  9. JAVA+SELENIUM+MAVEN+TESTNG框架(二)新建项目

    1.新建maven项目 2.下载selenium的jar包,放入maven依赖库中 3.新增testng依赖库,build path->add libirary->testng 4.查看自 ...

随机推荐

  1. ldd

    ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库 [diego@localhost ~/work/branch_dispatch_201511/rtqa_center/source ...

  2. JavaSE入门学习23:Java面向对象之构造方法

    学了JavaSE面向对象这一部分,也该对构造方法做一个总结了. 一构造方法 在多数情况下,初始化一个对象的终于步骤是去调用这个对象的构造方法. 构造方法负责对象的初始化工作,为 实例变量赋予合适的初始 ...

  3. csv读入数据,用julia/matplotlib/pyplot 画矢量图导入word中

    这是是用julia来实现画图.julia有三个画图库:Winston.Gadfly.PyPlot 这里用的是pyplot,事实上他是基于matplotlib的 1.首先在juno里安装两个库 juno ...

  4. 关于 equals()与hashcode()方法

    类重写了equals()方法也必须重写hashcode()方法,否则会导致该类无法与基于散列值的集合(HashMap.HashSet.HashTable)一起正常使用. hashcode()方法遵循的 ...

  5. win7 32位下安装MySQL出现的---1067系统错误---问题及解决

    每次安装数据库,总是出现这样那样的问题.如今记录下来,供日后參考咯.... 下载的是解压缩-zip版本号的.安装配置教程參照洪哥笔记文章-<MySQL-5.6.13解压版(zip版)安装配置教程 ...

  6. [IT学习]转载python 项目 计算器

    这个是从网上搜到的Python小项目之计算器(原文地址:http://www.2cto.com/kf/201402/279637.html).但该段代码估计是Python 2 写的. 如果你使用的程序 ...

  7. ASP.NET MVC 原理

    我出了份卷子做面试题,其中之一就是要求说说ASP.NET MVC的原理.感觉太空泛了,谁能说得准呢? 但是,如果站在我这个面试官立场,面试题好多时并不要求有标准答案,可能也没有什么标准答案,主要是通过 ...

  8. 【转】Java 并发编程:volatile的使用及其原理

    一.volatile的作用 在<Java并发编程:核心理论>一文中,我们已经提到过可见性.有序性及原子性问题,通常情况下我们可以通过Synchronized关键字来解决这些个问题,不过如果 ...

  9. Spring 各种注解(@)的含义与认识

    依赖注入,从字面上理解,即是:以注入的方式实现依赖: Spring 容器负责创建应用程序中的 bean,并通过 DI(依赖注入)来协调这些对象之间的关系.当描述 bean 如何进行装配(autowir ...

  10. Ural 2003: Simple Magic(数论&思维)

    Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is e ...