在编写测试的过程中,我们经常遇到只想执行个别或者某一部分/某一类型的测试用例,这时我们可以使用TestNG的分组测试方法。
代码:
public class GroupTestExample {
@Test(groups={"group1","group2"})
public void f1(){
System.out.println("f1() grou is group1 and group2");
}
@Test(groups={"group1"})
public void f2(){
System.out.println("f2() grou is group1");
}
@Test(groups={"group2"})
public void f3(){
System.out.println("f3() grou is group2");
}
@Test(groups={"group1","group2"})
public void f4(){
System.out.println("f4() grou is group1 and group2");
}
}
testng.xml文件配置
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="Test">
<groups>
<run>
<include name="group2"/>
</run>
</groups>
<classes>
<class name="com.testcase.GroupTestExample"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
运行结果:
[TestNG] Running:
F:\android\android_work\Test2\src\testng.xml
f1() grou is group1 and group2
f3() grou is group2
f4() grou is group1 and group2
===============================================
Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
组中组
代码:
public class TestngGroupsOfGroups {
@Test(groups = { "windows.xp" })
public void testMethod5() {
System.err.println("(groups = { windows.xp })");
}
@Test(groups = { "windows.7" })
public void testMethod6() {
System.err.println("(groups = { windows.7 })");
}
@Test(groups = { "windows.8" })
public void testMethod7() {
System.err.println("(groups = { windows.8 })");
}
}
testng.xml配置
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
- <suite name="framework_testng">
- <test verbose="2" name="TestGroupsOfGroups">
- <groups>
- <define name="windows.xp">
- <include name="windows.xp" />
- </define>
- <define name="windows.7">
- <include name="windows.7" />
- </define>
- <define name="all">
- <include name="windows.*" />
- </define>
- <run>
- <include name="all" />
- <exclude name="windows.7" />
- </run>
- </groups>
- <classes>
- <class name="com.dragon.testng.annotation.TestngGroupsOfGroups" />
- </classes>
- </test>
- </suite>
- (groups = { windows.xp })
- (groups = { windows.8 })
- PASSED: testMethod5
- PASSED: testMethod7
- ===============================================
- TestGroupsOfGroups
- Tests run: 2, Failures: 0, Skips: 0
- ===============================================