
- TestNG 教程
- TestNG - 首頁
- TestNG - 概述
- TestNG - 環境配置
- TestNG -編寫測試用例
- TestNG - 基本註解
- TestNG - 執行流程
- TestNG - 執行測試
- TestNG - 套件測試
- TestNG - 忽略測試
- TestNG - 組測試
- TestNG - 異常測試
- TestNG - 依賴測試
- TestNG - 引數化測試
- TestNG - 執行JUnit測試
- TestNG - 測試結果
- TestNG - 註解轉換器
- TestNG - 斷言
- TestNG - 並行執行
- TestNG - 與ANT整合
- TestNG - 與Eclipse整合
- TestNG - TestNG vs JUnit
- TestNG 有用資源
- TestNG - 快速指南
- TestNG - 有用資源
- TestNG - 討論
TestNG - 基本註解 - BeforeGroups
@BeforeGroups 註解的方法只會在所有屬於指定組的測試方法執行之前執行一次。
以下是@BeforeGroups註解支援的屬性列表
屬性 | 描述 |
---|---|
alwaysRun |
對於 before 方法(beforeSuite、beforeTest、beforeTestClass 和 beforeTestMethod,但不包括 beforeGroups):如果設定為 true,則無論此配置方法屬於哪些組,都會執行。 對於 after 方法(afterSuite、afterClass……):如果設定為 true,即使之前呼叫的一個或多個方法失敗或被跳過,此配置方法也會執行。 |
dependsOnGroups |
此方法依賴的組列表。 |
dependsOnMethods |
此方法依賴的方法列表。 |
enabled |
此類/方法上的方法是否啟用。 |
groups |
此類/方法所屬的組列表。 |
inheritGroups |
如果為 true,則此方法將屬於類級別 @Test 註解中指定的組。 |
onlyForGroups |
僅適用於 @BeforeMethod 和 @AfterMethod。如果指定,則只有當相應的測試方法屬於列出的組之一時,才會呼叫此設定/拆卸方法。 |
建立類
建立一個要測試的 Java 類,例如,在/work/testng/src 中建立MessageUtil.java。
/* * This class prints the given message on console. */ public class MessageUtil { private String message; //Constructor //@param message to be printed public MessageUtil(String message) { this.message = message; } // prints the message public String printMessage() { System.out.println(message); return message; } }
建立測試用例類
建立一個 Java 測試類,例如,在/work/testng/src 中建立TestAnnotationBeforeGroups.java。
向測試類新增測試方法 testMethod()。
向 testMethod() 方法添加註解 @Test,並將此方法新增到組testOne。
向測試類新增一個帶有註解 @BeforeGroups 的 beforeGroups 方法,並透過添加註解@BeforeGroups("testOne")在testOne組之前執行它。
實現測試條件並檢查 @BeforeGroups 註解的行為。
以下是TestAnnotationBeforeGroups.java的內容
import org.testng.Assert; import org.testng.annotations.Test; import org.testng.annotations.BeforeGroups; public class TestAnnotationBeforeGroups { MessageUtil messageUtil = new MessageUtil("Test method"); @BeforeGroups("testOne") public void beforeGroups(){ System.out.println("beforeGroups method executed before testOne group"); } @Test(groups={"testOne"}) public void testMethod(){ Assert.assertEquals("Test method helooo", messageUtil.printMessage()); } }
建立testng.xml
接下來,讓我們在/work/testng/src中建立 testng.xml 檔案來執行測試用例。此檔案以 XML 格式捕獲您的整個測試過程。此檔案使您可以輕鬆地在單個檔案中描述所有測試套件及其引數,您可以將其檢入程式碼儲存庫或透過電子郵件傳送給同事。它還可以輕鬆提取測試的子集或拆分多個執行時配置(例如,testngdatabase.xml 只執行測試您的資料庫的測試)。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name="Suite"> <test thread-count="5" name="Test"> <classes> <class name="TestAnnotationBeforeGroups"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
使用 javac 編譯測試用例。
/work/testng/src$ javac TestAnnotationBeforeGroups.java MessageUtil.java
現在,執行 testng.xml,它將執行在 <test> 標籤中定義的測試用例。正如您所看到的,@BeforeGroups 在所有其他測試用例之前被呼叫。
/work/testng/src$ java org.testng.TestNG testng.xml
驗證輸出。
beforeGroups method executed before testOne group Test method =============================================== Suite Total tests run: 1, Passes: 0, Failures: 1, Skips: 0 ===============================================