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 ...
随机推荐
- LG3092 「USACO2013NOV」No Change 状压DP
问题描述 https://www.luogu.org/problem/P3092 题解 观察到 \(k \le 16\) ,自然想到对 \(k\) 状压. 设 \(opt[i]\) 代表使用硬币状况为 ...
- 给那些迷茫的人学习JAVA的一些建议?
前语:我用了3年的时间,一步一步走到了现在,半途也有了解过其他的技能,也想过要转其他的言语,可是最终仍是坚持下来走Java这条路,希望我的经历能够帮忙到后来的人,要是觉得对你有帮忙的话,能够注重一下和 ...
- linux umask计算方法
1. umask用于设定默认的新建文件或目录的权限 查看umask当前值命令: umask -p 计算创建出的file权限方法: 如果umask值的每位数都是偶数,使用666按位减umask的值即可 ...
- java(二)变量
基础数据类型: 数值型:整型(byte.short.int.long).浮点型(float.double)java各整数类型有固定的表数范围和字段长度,不受具体os的影响,以保持java的可移植性:j ...
- #3144. 「APIO 2019」奇怪装置
#3144. 「APIO 2019」奇怪装置 题目描述 考古学家发现古代文明留下了一种奇怪的装置.该装置包含两个屏幕,分别显示两个整数 \(x\) 和 \(y\). 经过研究,科学家对该装置得出了一个 ...
- vsc 自定义快速生成vue模板
1.安装vscode 官网地址:https://code.visualstudio.com/ 2.安装一个插件,识别vue文件 插件库中搜索Vetur,下图中的第一个,点击安装,安装完成之后点击重新加 ...
- php使用inotify扩展监控文件或目录,如果发生改变,就执行指定命令
通过inotify扩展监控文件或目录的变化,如果发生变化,就执行命令. 可以应用于 swoole 中,如果文件发生变化,就执行 kill -USR1 进程PID 来实现热更新. <?php cl ...
- Java开发桌面程序学习(11)——javafx 鼠标点击,右击,双击
javafx 鼠标事件 给某个控件设置鼠标点击监听器,三个条件分别判断为单击,右击还是双击 单击判断 event.getButton()==MouseButton.PRIMARY 右击判断 event ...
- python读取Excel的值
上代码: import pandas as pd if __name__ == '__main__': #默认的读取第一个sheet df = pd.read_excel("E:\\MyPr ...
- PlayJava Day010
今日所学: /* 2019.08.19开始学习,此为补档. */ 1.继承补充: ①不要仅为了获取其他类中某个功能而去继承,而是要有所属关系. ②Super关键字: a.代表父类对象的引用,且main ...