http://blog.csdn.net/d6619309/article/details/52435084

一、概述

TestNG是一个开源自动化测试框架,其灵感来自JUnit和NUnit,TestNG还涵盖了整个核心的JUnit4功能,但引入了一些新的功能,使其功能更强大,使用更方便。

优势:支持依赖测试方法,并行测试,负载测试,局部故障;灵活的插件API;支持多线程测试;

二、常用注解

TestNG的注解大部分用在方法级别上。常用的注解列举如下:

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注解的方法,将会在当前测试类的每一个测试方法执行之后运行。

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

新建TestNGAnnotationTest和TestNGAnnotationTest2(TestNGAnnotationTest2与TestNGAnnotationTest内容一致)

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");
} }

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>

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

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

2. @Test 注解

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

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

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

常见取值说明如下:

  • 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");
} }

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);
} }

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>

4. @DataProvider 注解

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


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")};
} }
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);
}
}

testng.xml配置:

<test name="testFactory">
<classes>
<class name="com.crazypig.testngdemo.AnnotationFactoryTest" />
</classes>
</test>

运行结果:

SimpleTest.param = one
SimpleTest.param = two

6. @Listeners 注解

TestNG丰富的Listener监听器满足不同阶段的监听。

这个注解必须定义在类、接口或者枚举类级别。实用的Listener包括

IAnnotationTransformer  
  IAnnotationTransformer2  
  IHookable  
  IInvokedMethodListener  
  IMethodInterceptor  
  IReporter  
  ISuiteListener  
  ITestListener

他们可以在suite级别、test级别和test method一些执行点执行一些自定义操作,如打印日志。

参考资料

【转】TestNG常用注解的更多相关文章

  1. TestNG常用注解

    原文链接:https://www.yiibai.com/testng/basic-annotations.html    以下是TestNG支持的注释列表: 注解 描述 @BeforeSuite 在该 ...

  2. testng 的常用注解

    常用注解如下: @BeforeSuite: 此注解的方法会在当前测试集合中的任一测试用例前执行 @AfterSuite: 此注解的方法会在当前测试集合中的所有测试程序结束后执行 @BeforeTest ...

  3. testNG 常用的注解

    常用注解介绍: @BeforeSuite 在该套件的所有测试都运行在注释的方法之前,仅运行一次 @AftereSuite 在该套件的所有测试都运行在注释方法之后,仅运行一次 @BeforeClass  ...

  4. TestNG基本注解

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

  5. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  6. SpringMVC常用注解實例詳解3:@ResponseBody

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  7. SpringMVC常用注解實例詳解2:@ModelAttribute

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  8. Spring常用注解汇总

    本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下: 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包). <context:component- ...

  9. Spring常用注解,自动扫描装配Bean

    1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...

随机推荐

  1. 基于mysql数据库 关于sql优化的一些问题

    mysql数据库有一个explain关键词,可以对select语句进行分析并且输出详细的select执行过程的详细信息. 对sql explain后输出几个字段: id:SELECT查询的标识符,每个 ...

  2. JavaScript中的内置函数

    JavaScript中的内置函数 制作人:全心全意 在使用JavaScript语言时,除了可以自定义函数之外,还可以使用JavaScript的内置函数,这些内置函数是由JavaScript语言自身提供 ...

  3. 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)

    NOTE: 1.Copying 函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 2.不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个 ...

  4. LeetCode(52) N-Queens II

    题目 Follow up for N-Queens problem. Now, instead outputting board configurations, return the total nu ...

  5. cherrypy & gevent patch

    给cherrypy 打gevent WSGIServer的patch 1. patch Serving 类 2. 关闭python的原生WSGIServer 具体使用例子参考 我的开源项目  http ...

  6. python request包使用指西

    request是Python的一个网络模块包,使用它可以快速写一些强大的爬虫脚本

  7. python和搜索

    # -*- coding: UTF-8 -*- import re # 搜索逻辑 def querylogic(list): query = {} if len(list) > 1 or len ...

  8. CodeForces230A

    题目大意: 一个打恐龙的游戏,有初始体力s,和恐龙n只,然后输入n只恐龙的体力xi,和击杀它得到的奖励体力yi,只有自身体力大于恐龙体力时才能进行击杀,击杀恐龙的顺序可以不定 这题运用到了贪心的思想, ...

  9. 【模板】prim的heap优化

    简单的代码.. 时间复杂度为O((n + m)logn) 大部分情况下还是跑不过kruskal的,慎用. #include <cstdio> #include <queue> ...

  10. codeforces 303 div2 E

    赤裸裸的最短路,需要注意下枚举过程就好了.直接贴上别人的代码,发现他的代码挺符合我的风格,以后就这样写了. #include <bits/stdc++.h> ]; ]; ]; ]; vec ...