分为方法的分组和类的分组:
GroupsOnMethod类:
package com.janson.groups; import org.testng.annotations.Test; public class GroupsOnMethod { @Test(groups = "server")
public void serverTest1() {
System.out.println("这是服务端组测试方法1");
} @Test(groups = "server")
public void serverTest2() {
System.out.println("这是服务端组测试方法2");
} @Test(groups = "client")
public void clientTest1() {
System.out.println("这是客户端组测试方法1111");
} @Test(groups = "client")
public void clientTest2() {
System.out.println("这是客户端组测试方法2222");
} @Test(groups = "thirdParty")
public void thirdPartyTest1() {
System.out.println("这是第三方组测试方法1111");
} @Test(groups = "thirdParty")
public void thirdPartyTest2() {
System.out.println("这是第三方组测试方法2222");
}
}
类的分组分为三个类:
GroupsOnClass1类:
package com.janson.groups; import org.testng.annotations.Test; @Test(groups = "stu")
public class GroupsOnClass1 { public void stu1() {
System.out.println("GroupsOnClass1中的stu1运行!");
} public void stu2() {
System.out.println("GroupsOnClass1中的stu2运行!");
}
}
GroupsOnClass2类:
package com.janson.groups; import org.testng.annotations.Test; @Test(groups = "stu")
public class GroupsOnClass2 { public void stu1() {
System.out.println("GroupsOnClass2中的stu1运行!");
} public void stu2() {
System.out.println("GroupsOnClass2中的stu2运行!");
}
}
GroupsOnClass3类:
package com.janson.groups; import org.testng.annotations.Test; @Test(groups = "teacher")
public class GroupsOnClass3 { public void teacher1() {
System.out.println("GroupsOnClass3中的teacher1运行!");
} public void teacher2() {
System.out.println("GroupsOnClass3中的teacher2运行!");
}
}
在resources文件夹下新建一个groups.xml文件:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="groupSuite" preserve-order="true">
<test name="groupOnMethodTest">
<groups>
<run>
<!-- 不包含某个分组 -->
<exclude name="client"/>
</run>
</groups>
<classes>
<class name="com.janson.groups.GroupsOnMethod"/>
</classes>
</test>
<test name="groupOnClassTest">
<groups>
<run>
<!-- 包含某个分组 -->
<include name="teacher"/>
</run>
</groups>
<classes>
<class name="com.janson.groups.GroupsOnClass1"/>
<class name="com.janson.groups.GroupsOnClass2"/>
<class name="com.janson.groups.GroupsOnClass3"/>
</classes>
</test>
</suite>
执行结果如下:
这是服务端组测试方法1
这是服务端组测试方法2
这是第三方组测试方法1111
这是第三方组测试方法2222
GroupsOnClass3中的teacher1运行!
GroupsOnClass3中的teacher2运行!