Testng 简介
Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性:
- annotations 注释,如 @test @BeforeMethod
- 支持多线程执行case
- 支持数据驱动 dataProvider
- 支持参参数
- 能够作为eclipse的插件
- 能够(配合reportng)生产客观的测试报告
- 可通过testng.xml管理执行case和suite
那么好的测试框架,怎么使用?
这里我们使用eclipse插件方式 安装详见:http://testng.org/doc/eclipse.html
testng使用
首先了解一下testng 的annotations
常见的有以下:
@BeforeClass: 该annotation在class激活之前执行
@BeforeMethod: 该annotation会在每个执行的方法之前执行
@Test ,该annotation 是你要执行测试的方法
@AfterMethod,该annotation在每个测试方法执行之后运行
@AfterClass 该annotation会在所有测试方法之后运行
具体生命周期如下图:
这里是所有的annotation
@BeforeSuite @AfterSuite @BeforeTest @AfterTest @BeforeGroups @AfterGroups @BeforeClass @AfterClass @BeforeMethod @AfterMethod |
Configuration information for a TestNG class:
@BeforeSuite: The annotated method will be run before all tests in this suite have run. |
实例:我们验证一下testng annotation 执行顺序,这个case里有两个 测试
,执行顺序为beforeClass->beforeMethod->test1->afterMethod->beforeMethod->
test2->afterMethod->afterClass.

package com.dbyl.tests; import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* This is to verify testng annotation execute
* @author Young
*
*/
public class TestngExample {
private int a; @BeforeMethod(alwaysRun=true)
public void beforeMethod() {
a = 2;
System.out.println("This is beforeMethod method. The Value of a is: "
+ a);
} @BeforeClass
public void beforeClass() {
a = 1;
System.out.println("This is beforeClass method .The Value of a is: "
+ a);
} @Test(groups = "TestngExample")
public void testExample1() {
a = 3;
System.out.println("This is Test method1 .The Value of a is: " + a);
} @Test(groups = "TestngExample")
public void testExample2() {
a = 4;
System.out.println("This is Test method2 .The Value of a is: " + a);
} @AfterClass
public void afterClass() {
a = 5;
System.out.println("This is AfterClass Method .The Value of a is: " + a); } @AfterMethod
public void afterMethod()
{
a = 6;
System.out.println("This is AfterMethod Method .The Value of a is: " + a);
} }

所以执行结果为:
1
2
3
4
5
6
7
8
9
10
|
This is beforeClass method .The Value of a is: 1 This is beforeMethod method. The Value of a is: 2 This is Test method1 .The Value of a is: 3 This is AfterMethod Method .The Value of a is: 6 This is beforeMethod method. The Value of a is: 2 This is Test method2 .The Value of a is: 4 This is AfterMethod Method .The Value of a is: 6 This is AfterClass Method .The Value of a is: 5 PASSED: testExample1 PASSED: testExample2 |
当然,还有BeforeSuite 等,不再做深入研究.
annotation后面可以加一些参数,比如alwaysRun=true/false,dependsOnMethods=""
alwaysRun控制是否每次都执行,dependsOnMethods是一种依赖,依赖某个方法执行
之前有提到testng数据驱动,使用dataProvider,dataProvider可以导入text ,excel等数据,
执行case时会从DataProvider依次拿出数据执行,同一个测试方法,会被执行相应的次数

package com.dbyl.tests; import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test; public class Case1 { @DataProvider
public Object[][] testData1() {
return new Object[][] { { 1, 2, 3 }, { 1, 2, 4 }, { 1, 3, 4 },
{ -1, 3, 2 } };
} @DataProvider
public Object[][] testData2() {
return new Object[][] { { 5, 2, 3 }, { 1, 2, 4 }, { 1, -3, 4 },
{ 6, 3, 2 } };
} public static int add(int a, int b) {
return a + b;
} public static int minus(int a, int b) {
return a - b;
} @BeforeClass
public void beforeClass()
{
System.out.println("This is Before Class");
}
@Test(groups = { "add" }, dataProvider = "testData1")
public void addTest(int a, int b, int c) {
System.out.println("This is test add method. "+a+" + "+ b+" = "+c);
Assert.assertEquals(add(a, b), c);
} @Test(groups = { "minus" }, dataProvider = "testData2")
public void minusTest(int a, int b, int c) {
System.out.println("This is test minus method. "+a+" - "+ b+" = "+c);
Assert.assertEquals(minus(a, b), c);
} @BeforeMethod
public void beforeMethod()
{
System.out.println("This is Before Method");
} @AfterMethod
public void afterMethod()
{
System.out.println("This is After Method");
} @AfterClass
public void afterClass()
{
System.out.println("This is After Class");
}
}

执行结果如下:

This is Before Class
This is Before Method
This is test add method. 1 + 2 = 3
This is After Method
This is Before Method
This is test add method. 1 + 2 = 4
This is After Method
This is Before Method
This is test add method. 1 + 3 = 4
This is After Method
This is Before Method
This is test add method. -1 + 3 = 2
This is After Method
This is Before Method
This is test minus method. 5 - 2 = 3
This is After Method
This is Before Method
This is test minus method. 1 - 2 = 4
This is After Method
This is Before Method
This is test minus method. 1 - -3 = 4
This is After Method
This is Before Method
This is test minus method. 6 - 3 = 2
This is After Method
This is After Class
PASSED: addTest(1, 2, 3)
PASSED: addTest(1, 3, 4)
PASSED: addTest(-1, 3, 2)
PASSED: minusTest(5, 2, 3)
PASSED: minusTest(1, -3, 4)
FAILED: addTest(1, 2, 4)

由于天色已晚,明天继续总结.
接下来:testng.xml的配置和使用,已经参数传递
testng配合reportng 生成测试报告
testng 配合ant
转载请注明出处:http://www.cnblogs.com/tobecrazy/
Testng 简介的更多相关文章
- APP接口自动化测试JAVA+TestNG(二)之TestNG简介与基础实例
前言 继上篇环境篇后,本篇主要对TestNG进行介绍,给出最最基础的两个实例,通过本文后,学会并掌握TestNG测试用例的编写与运行,以及生成美化后的报告.下一篇为HTTP接口实战(国家气象局接口自动 ...
- TestNG简介与安装步骤
简述 TestNG是一个设计用来简化广泛的测试需求的测试框架, 从单元测试(隔离测试一个类) 到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统, 例如运用服务器) . testNG灵感来 ...
- UI自动化测试(五)TestNG简介与安装步骤
简述 TestNG是一个设计用来简化广泛的测试需求的测试框架, 从单元测试(隔离测试一个类) 到集成测试(测试由有多个类多个包甚至多个外部框架组成的整个系统, 例如运用服务器) . testNG灵感来 ...
- testng 教程
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- 一个简单的TestNG例子
关于TestNG好的资源: 官网文档:http://testng.org/doc/documentation-main.html 一 下载并安装:1. JDK 1.7 $ java -version ...
- eclipse集成testng插件
一.TestNG简介 TestNG是一个开源自动化测试框架,它受到JUnit和NUnit的启发,而引入了许多新的创新功能,如依赖测试,分组概念,使测试更强大,更容易做到. 它旨在涵盖所有类别的测试:单 ...
- 总结TestNg与JUnit的异同
工作中一直用的是junit,近期稍微学习了一下TestNg,发现TestNg比java强大太多. TestNg简介 TestNg也是一套测试框架,它的灵感来源于Junit(java的单元测试框架)和N ...
- TestNG基本使用
TestNG简介 Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation 创建maven项目,添加依赖 <dependency> ...
- Testng1
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
随机推荐
- WINDOWS代理服务器搭建 - Apache httpd
1.检查电脑系统类型 检查电脑版本是为 32位操作系统 还是 64位操作系统 2.下载安装Apache Httpd 下载地址:https://www.apachehaus.com/cgi-bin/do ...
- 使用Runtime的objc_copyClassNamesForImage和objc_getClassList获取类
一.介绍 objc_copyClassNamesForImage:拷贝动态库类列表,也即当前工程下自己创建的所有类 objc_getClassList:获取所有类列表,也即当前工程下所有类(含系统类. ...
- CSS修改选中文本颜色与背景色
壹 ❀ 引 在做博客美化的时候,想着去修改文本选中的背景色,因为网页默认是蓝底白字,看着与自己博客整体配色不太搭配,所以想着去改改. 贰 ❀ ::selection 解决方案其实很简单,使用css ...
- 融云技术分享:解密融云IM产品的聊天消息ID生成策略
本文来自融云技术团队原创分享,原文发布于“融云全球互联网通信云”公众号,原题<如何实现分布式场景下唯一 ID 生成?>,即时通讯网收录时有部分改动. 1.引言 对于IM应用来说,消息ID( ...
- JAVA 网络编程 - 实现 群聊 程序
在实现 这个 程序之前, 我们 需要 了解 一些 关于 Java 网络 编程 的 知识. 基本 的 网络知识: 网络模型 OSI (Open System Interconnection 开放系统互连 ...
- TCP协议 - 可靠性
在前篇文章中介绍了TCP协议的三大特性,其中可靠性是依赖一系列的机制,如:校验和,分组发送,超时重传,流量控制得到保证. 一.数据交互 TCP在交互数据时,采用多种机制保证可靠性,同时也保证TCP的性 ...
- Redis for OPS 03:数据安全与持久化
写在前面的话 通过前两节,除了安装部分,其它的更多的是作为了解,除非我们面向实际的开发,当然知道更多总是好的,这样才有吹牛逼的资本. 从本节开始我们主要谈谈作为一个运维,在处理 Redis 的维护的时 ...
- 一个小事例,了解golang通道阻塞模式
在学习golang中,channel真的是让人捉摸不透的东西,本来我自以为我理解了协程阻塞的用法了,结果就下面这个小例子,我还是在打印输出后才搞明白到底怎么回事? 当然了,这也是我自身对协程这块不太熟 ...
- Python【day 14-3】二分法查找
#二分法查找 #方法1 循环+左右边界变动,两者差减半 #方法2 递归+新列表长度减半 #方法3 递归+左右边界变动,两者差减半 #方法1 循环+左右边界变动,两者差减半 def recursion1 ...
- Java反射及注解
一.反射 1.动态语言:是指程序在运行是可以改变其结构:新的函数可以引进,已有的函数可以被删除等结构上的变化.比如常见的JavaScript就是动态语言,除此以外Python等也属于动态语言,而C.C ...