文章目錄
-
- 一、組測試中的方法分組測試
- 二、組測試中的類分組測試
- 三、組中組測試
groups分組,可以用在方法上,也可以用在類上。我們可以根據需要,靈活地對方法和類進行分組。
一、組測試中的方法分組測試
通過@Test注解的groups屬性,将一個測試類中的before/after方法分組執行。
import org.testng.annotations.AfterGroups;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test;
public class GroupsOnMethod {
@Test(groups = "server")
public void test1(){
System.out.println("服務端組的測試方法1");
}
@Test(groups = "server")
public void test2(){
System.out.println("服務端組的測試方法2");
}
@Test(groups = "client")
public void test3(){
System.out.println("用戶端組的測試方法3");
}
@Test(groups = "client")
public void test4(){
System.out.println("用戶端組的測試方法4");
}
@BeforeGroups("server")
public void beforeGroupsOnServer(){
System.out.println("服務端組運作之前執行的方法");
}
@AfterGroups("server")
public void afterGroupsOnServer(){
System.out.println("服務端組運作之後執行的方法");
}
@BeforeGroups("client")
public void beforeGroupsOnClient(){
System.out.println("用戶端組運作之前執行的方法");
}
@AfterGroups("client")
public void afterGroupsOnClient(){
System.out.println("用戶端組運作之後執行的方法");
}
}
執行結果如下:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL9UEVaFDetlFbsdlYoBnMMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL5cTOzAjNzgTM3ADOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
二、組測試中的類分組測試
組類要運作的類可以自定義,可以包括要執行的,排除不要執行的方法。必須和<classes>配套使用,從下面的類中找到對應名字的方法、
<groups>由<define>和<run>、<dependencies>三部分組成。
- <define>可以将group組成一個新組,包括要執行和不執行的大組;
- <run>要執行的方法;
- <dependencies>指定了某group需要依賴的group
GroupsOnClass1.java
package com.course.testng.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.java
package com.course.testng.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.java
package com.course.testng.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運作");
}
}
groupsOnClass.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
<suite name="suiteName">
<!--class1/2/3中的測試類都執行-->
<test name="runAll">
<classes>
<class name="com.course.testng.groups.GroupsOnClass1"/>
<class name="com.course.testng.groups.GroupsOnClass2"/>
<class name="com.course.testng.groups.GroupsOnClass3"/>
</classes>
</test>
<!--使用groups标簽,隻執行group name為stu的測試類-->
<test name="onlyRunStu">
<groups>
<run>
<include name="stu"/> <!--隻執行group name為stu的測試類-->
</run>
</groups>
<classes>
<class name="com.course.testng.groups.GroupsOnClass1"/>
<class name="com.course.testng.groups.GroupsOnClass2"/>
<class name="com.course.testng.groups.GroupsOnClass3"/>
</classes>
</test>
</suite>
運作groupsOnClass.xml,結果如下
三、組中組測試
分組測試在配置時,TestNG執行的原則是:隻保留最小集合進行執行
//使用groups進行分組測試,include和exclude的原則是保留最小集合
public class TestngGroups {
@Test(groups = { "functest", "checkintest" })
public void testMethod1() {
System.err.println("groups = { functest, checkintest }");
}
@Test(groups = { "functest", "checkintest" })
public void testMethod2() {
System.err.println("groups = { functest, checkintest }");
}
@Test(groups = { "functest" })
public void testMethod3() {
System.err.println("groups = { functest }");
}
@Test(groups = { "checkintest" })
public void testMethod4() {
System.err.println("groups = { checkintest }");
}
}
testng-groups.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="TestGroups">
<groups>
<run>
<include name="functest" />
<exclude name="checkintest" />
</run>
</groups>
<classes>
<class name="com.dragon.testng.annotation.TestngGroups" />
</classes>
</test>
</suite>
執行結果:
當我們的測試用例累積了很多以後,我們可能不需要測試之前的分組,隻要測試剛剛寫好的分組,這時候testng提供了一種新的配置方式,來實作這一功能,讓測試人員隻修改配置檔案就完成測試。
組中組,即在groups标簽内,使用<define>标簽在現有組名的基礎上,自定義一些分組(可以了解為按組名為機關,再一次分組),友善groups調用這些分組。比如下面例子中,define了名為all的組,包括組名windows.xp/windows.7/windows.8的測試類,此時在run中可以直接<include> all就可以完成組名為windows.xp/windows.7/windows.8的測試類的調用。
/**
* 使用<define>标簽将測試方法在組内再次進行分組并以name屬性進行區分,
* <run>通過define标簽的name進行調用,以後修改測試直接修改run調用的名稱即可
*/
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-groupOfGroups.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="xp">
<include name="windows.xp" />
</define>
<define name="w7">
<include name="windows.7" />
</define>
<define name="all">
<include name="windows.*" />
</define>
<run>
<include name="all" />
<exclude name="w7" />
</run>
</groups>
<classes>
<class name="com.dragon.testng.annotation.TestngGroupsOfGroups" />
</classes>
</test>
</suite>
測試結果:(注意:此時 被運作的測試分組将在run标簽内進行配置,include和exclude時,是根據Define标簽的name來決定)