testNG方法分組測試


testNG組測試可以分為方法分組測試類分組測試。

一、方法分組測試

方法的分組 主要是在@Test中添加groups參數。相同的groups參數值 為同一組,通過testng.xml的<groups>下的<run>下的<include>標簽中的name值調用。如果xml<include>標簽中的name和@Test中添加groups參數一致,則會運行。反之則不會運行。

代碼如下

 1 package com.course.testng.groups;
 2 
 3 import org.testng.annotations.AfterGroups;
 4 import org.testng.annotations.BeforeGroups;
 5 import org.testng.annotations.Test;
 6 
 7 public class GroupsOnMethod {
 8     @Test(groups = "server")
 9     public void test1() {
10         System.out.println("這是服務端的測試方法1");
11     }
12 
13     @Test(groups = "server")
14     public void test2() {
15         System.out.println("這是服務端的測試方法2");
16     }
17 
18     @Test(groups = "client")
19     public void test3() {
20         System.out.println("這是客戶端的測試方法3");
21     }
22 
23     @Test(groups = "client")
24     public void test4() {
25         System.out.println("這是客戶端的測試方法4");
26     }
27 
28     @BeforeClass("server")
29     public void beforeGroupsOnServier() {
30         System.out.println("這是服務端組運行之前運行的方法");
31     }
32 
33     @AfterGroups("server")
34     public void afterGroupsOnServier() {
35         System.out.println("這是服務端組運行之后運行的方法");
36     }
37 
38 }

testng.xml 如下

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <suite name="test">
 3     <test name="groupsmethod">
 4         <groups>
 5             <run>
 6                 <include name="server"/>
 7             </run>
 8         </groups>
 9         <classes>
10             <class name="com.course.testng.groups.GroupsOnMethod"/>
11         </classes>
12     </test>
13 </suite>

運行結果如下

 1   E:\Program Files\workspace\muke\AotoTest\chapter5\src\main\resources\groupsOnMethod.xml
 2 這是服務端的測試方法1
 3 
 4 這是服務端的測試方法2
 5 
 6 ===============================================
 7 test
 8 Total tests run: 2, Failures: 0, Skips: 0
 9 ===============================================
10 
11 Process finished with exit code 0

注意事項:

由以上執行結果可以看出來,@Test(groups = "server")會被執行,但@BeforeGroups("server")和@BeforeClass("server")都不會執行。如果需要執行可以使用always參數。如@AfterClass( alwaysRun = true)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM