TestNG测试依赖
两种依赖设置
- 依赖方法method
- 依赖组group
- 方法的依赖:testDemo1依赖testDemo2
@Test(groups = {"smoke", "uat"}, dependsOnMethods = {"testDemo2"})
public void testDemo1() throws InterruptedException{
System.out.print("testDemo1\n");
}
@Test(groups = {"smoke", "func"})
public void testDemo2() throws InterruptedException{
System.out.print("testDemo2\n");
}
testng.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<parameter name="username" value="testname" /> <!-- 添加参数 -->
<classes>
<class name="com.nitb.demo.Demo1">
<methods>
<include name="testDemo1" />
<include name="testDemo2" /> <!-- 这里看到testDemo2在testDemo1后面 -->
</methods>
</class>
</classes>
</test>
</suite>
从上面的xml看执行顺序应该是testDemo1–>testDemo2,但是由于设置了依赖关系,执行的顺序是testDemo2–>testDemo1,执行结果如下:
BeforeClass
testDemo2
testDemo1
AfterClass
===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================
- 组的依赖:testDemo1依赖group=start
@Test(groups = {"smoke", "uat"}, dependsOnMethods = {"testDemo2"}, dependsOnGroups = {"start"})
public void testDemo1() throws InterruptedException{
System.out.print("testDemo1\n");
}
@Test(groups = {"smoke", "func"})
public void testDemo2() throws InterruptedException{
System.out.print("testDemo2\n");
}
......
@Test(groups = {"start"})
public void testDemo5() {
System.out.print("testDemo5\n");
}
testng.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
<test name="Regression1">
<parameter name="username" value="testname" /> <!-- 添加参数 -->
<classes>
<class name="com.nitb.demo.Demo1">
<methods>
<include name="testDemo1" />
<include name="testDemo2" /> <!-- 这里看到testDemo2在testDemo1后面 -->
<include name="testDemo5" />
</methods>
</class>
</classes>
</test>
</suite>
运行结果:
testDemo2
testDemo5
testDemo1
===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================
从运行结果可以看到,testDemo1运行所依赖的group=start的testDemo5先运行了。这里testDemo1既依赖testDemo2(方法依赖)又依赖testDemo5(组依赖),那么这两个的执行顺序靠什么决定?假设testDemo2和testDemo5没有关系,那么执行顺序就按照中的排列顺序来执行。
随机运行suite中的测试用例
- 增加一个实现IMethodInterceptor的方法
public class TestOrderRandomizer implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> list, ITestContext iTestContext) {
long seed = System.nanoTime();
Collections.shuffle(list, new Random(seed));
return list;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1" verbose="1" >
<listeners>
<listener class-name="com.nitb.demo.TestOrderRandomizer" />
</listeners>
<test name="Regression1">
<classes>
<class name="com.nitb.demo.Demo2">
</class>
</classes>
</test>
</suite>
执行的过程中,拦截所有TestNG加载的的方法,然后使用Collections.shuffle打乱列表中的顺序,返回给运行器执行。
多次运行结果:
第一次
BeforeClass
testDemo2
testDemo3
testDemo1
AfterClass
===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================
第二次
testDemo3
testDemo2
testDemo1
AfterClass
===============================================
Suite1
Total tests run: 3, Failures: 0, Skips: 0
===============================================
从上面的结果,可以看出,两次运行出来的结果顺序是不同的,随机的。
TestNG设置多个suite进行运行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="ETS Suite">
<suite-files>
<suite-file path="test-suite1.xml" />
<suite-file path="test-suite2.xml" />
</suite-files>
</suite>