目录
(一)TestNG学习之路—HelloWorld入门
(二)TestNG学习之路—注解及属性概览
(三)TestNG学习之路—TestNG.xml/YAML
(四)TestNG学习之路—注解详述之@Test
(五)TestNG学习之路—注解详述之参数化
(六)TestNG学习之路—注解详述之@Factory
(七)TestNG学习之路—注解详述之忽略测试
(八)TestNG学习之路—注解详述之并发
(九)TestNG学习之路—失败测试重跑
(十)TestNG学习之路—编码执行TestNG
(十一)TestNG学习之路—BeanShell高级用法
(十二)TestNG学习之路—注解转换器
(十三)TestNG学习之路—方法拦截器
(十四)TestNG学习之路—TestNG监听器
(十五)TestNG学习之路—依赖注入
(十六)TestNG学习之路—测试报告
(十七)基于TestNG+Rest Assured+Allure的接口自动化测试框架
前言
TestNG支持多线程(并发)的方式执行测试,相对于传统的单线程方式,一个显而易见的优势便是提升测试执行效率。
并发
TestNG官方文档给出了以下几种并发执行测试的方式。
1.suite并发
一个xml文件只包含一个suite标签,如果想多线程执行不同的suite,可通过以下方式来实现。
java org.testng.TestNG -suitethreadpoolsize 3 testng1.xml testng2.xml testng3.xml
2.tests,classes,methods,instances并发
testng.xml测试套件下的test,classe,method级别的并发,可通过以下方式配置。
- parallel="tests":TestNG将在同一个线程中运行相同test标签中的所有方法,不同test 标签下的用例在不同的线程执行。
<suite name="My suite" parallel="tests" thread-count="5">
示例:
编写parallel1,parallel2,parallel3类如下。
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class parallel1 {
@BeforeClass
public void bfClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 BeforeClass threadId: "+id);
}
@Test
public void test(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 Test threadId: "+id);
}
@AfterClass
public void afClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 AfterClass threadId: "+id);
}
}
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class parallel2 {
@BeforeClass
public void bfClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel2 BeforeClass threadId: "+id);
}
@Test
public void test(){
long id = Thread.currentThread().getId();
System.out.println("parallel2 Test threadId: "+id);
}
@AfterClass
public void afClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel2 AfterClass threadId: "+id);
}
}
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class parallel3 {
@BeforeClass
public void bfClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel3 BeforeClass threadId: "+id);
}
@Test
public void test(){
long id = Thread.currentThread().getId();
System.out.println("parallel3 Test threadId: "+id);
}
@AfterClass
public void afClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel3 AfterClass threadId: "+id);
}
}
testng.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="tests">
<test name="test1">
<classes>
<class name="parallel1"/>
<class name="parallel3"/>
</classes>
</test>
<test name="test2">
<classes>
<class name="parallel2"/>
</classes>
</test>
</suite>
执行结果如下:
parallel1 BeforeClass threadId: 9
parallel2 BeforeClass threadId: 10
parallel2 Test threadId: 10
parallel1 Test threadId: 9
parallel2 AfterClass threadId: 10
parallel1 AfterClass threadId: 9
parallel3 BeforeClass threadId: 9
parallel3 Test threadId: 9
parallel3 AfterClass threadId: 9
===============================================
All Test Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
根据执行结果输出的线程id可以判断:TestNG将在同一个线程中运行相同test标签中的所有方法,不同test 标签下的用例在不同的线程执行。
- parallel="classes":TestNG将在同一个线程中运行同一个class中的所有方法,但每个class将在单独的线程中运行。
<suite name="My suite" parallel="classes" thread-count="5">
示例:
修改testng.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="classes">
<test name="test1">
<classes>
<class name="parallel1"/>
<class name="parallel3"/>
</classes>
</test>
<test name="test2">
<classes>
<class name="parallel2"/>
</classes>
</test>
</suite>
执行结果:
parallel3 BeforeClass threadId: 10
parallel1 BeforeClass threadId: 9
parallel1 Test threadId: 9parallel3 Test threadId: 10
parallel3 AfterClass threadId: 10
parallel1 AfterClass threadId: 9
parallel2 BeforeClass threadId: 11
parallel2 Test threadId: 11
parallel2 AfterClass threadId: 11
===============================================
All Test Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
如上所示,不同的class在不同的线程中执行,同一class的所有方法在同一线程中执行。
- parallel="methods":TestNG将在单独的线程中运行所有测试方法。依赖方法也将在单独的线程中运行,但仍遵从依赖关系执行。
<suite name="My suite" parallel="methods" thread-count="5">
示例:
paralle1类增加test1方法:
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class parallel1 {
@BeforeClass
public void bfClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 BeforeClass threadId: "+id);
}
@Test
public void test(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 Test threadId: "+id);
}
@Test
public void test1(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 Test1 threadId: "+id);
}
@AfterClass
public void afClass(){
long id = Thread.currentThread().getId();
System.out.println("parallel1 AfterClass threadId: "+id);
}
}
修改testng.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="methods">
<test name="test1">
<classes>
<class name="parallel1">
<methods>
<include name="test"/>
<include name="test1"/>
</methods>
</class>
<class name="parallel3"/>
</classes>
</test>
<test name="test2">
<classes>
<class name="parallel2"/>
</classes>
</test>
</suite>
执行结果如下:
parallel3 BeforeClass threadId: 11
parallel1 BeforeClass threadId: 9
parallel1 Test threadId: 9
parallel3 Test threadId: 11
parallel1 Test1 threadId: 10
parallel3 AfterClass threadId: 11
parallel1 AfterClass threadId: 9
parallel2 BeforeClass threadId: 12
parallel2 Test threadId: 12
parallel2 AfterClass threadId: 12
===============================================
All Test Suite
Total tests run: 4, Failures: 0, Skips: 0
===============================================
如上结果所示,可发现每一个被@Test标注的方法都运行在单独的线程。
- parallel="instances":TestNG将在同一个线程中运行同一个实例中的所有方法,但是不同实例的方法将在不同的线程中运行。
<suite name="My suite" parallel="instances" thread-count="5">
示例:
修改parallel1类如下:
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class parallel1 {
@BeforeClass
public void bfClass(){
long id = Thread.currentThread().getId();
System.out.println(this.toString()+" "+ "parallel1 BeforeClass threadId: "+id);
}
@Test
public void test(){
long id = Thread.currentThread().getId();
System.out.println(this.toString()+" "+ "parallel1 Test threadId: "+id);
}
@Test
public void test1(){
long id = Thread.currentThread().getId();
System.out.println(this.toString()+" "+ "parallel1 Test1 threadId: "+id);
}
@AfterClass
public void afClass(){
long id = Thread.currentThread().getId();
System.out.println(this.toString()+" "+ "parallel1 AfterClass threadId: "+id);
}
}
增加一个工厂类TestParallel1Factory 如下:
import org.testng.annotations.Factory;
public class TestParallel1Factory {
@Factory
public Object[] testParallel1(){
Object[] objects = new Object[3];
for(int i=0;i<3;i++){
objects[i] = new parallel1();
}
return objects;
}
}
修改testng.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="instances" >
<test name="test">
<classes>
<class name="TestParallel1Factory"/>
</classes>
</test>
</suite>
执行结果如下:
parallel1@1ff8b8f parallel1 BeforeClass threadId: 9
parallel1@1c6a36e parallel1 BeforeClass threadId: 11
parallel1@7c703b parallel1 BeforeClass threadId: 10
parallel1@1ff8b8f parallel1 Test threadId: 9
parallel1@7c703b parallel1 Test threadId: 10
parallel1@1c6a36e parallel1 Test threadId: 11
parallel1@7c703b parallel1 Test1 threadId: 10
parallel1@1ff8b8f parallel1 Test1 threadId: 9
parallel1@1c6a36e parallel1 Test1 threadId: 11
parallel1@1c6a36e parallel1 AfterClass threadId: 11
parallel1@1ff8b8f parallel1 AfterClass threadId: 9
parallel1@7c703b parallel1 AfterClass threadId: 10
===============================================
All Test Suite
Total tests run: 6, Failures: 0, Skips: 0
===============================================
如上结果所示,每个实例的所有方法都运行在同一个线程内,但是不同实例运行在不同线程内。
3.同一个方法的并发
示例如下,各属性的用法在前面的文章有详细介绍。
@Test(threadPoolSize = 3, invocationCount = 10, timeOut = 10000)
public void testServer() {
}
4.dataProvider并发
《(五)TestNG学习之路—注解详述之参数化》文章末有提及,此处不再详述。