
- 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與JUnit比較
- TestNG 有用資源
- TestNG - 快速指南
- TestNG - 有用資源
- TestNG - 討論
TestNG - 基本註解 - BeforeMethod
使用`@BeforeMethod`註解的方法將在每個測試方法之前執行,例如,如果有三個測試方法(即測試用例),則`@BeforeMethod`註解的方法將分別在每個測試方法之前呼叫三次。
`@BeforeMethod`註解支援以下屬性列表:
屬性 | 描述 |
---|---|
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`中建立`TestAnnotationBeforeMethod.java`。
向測試類新增測試方法`testMethod()`。
向`testMethod()`方法新增`@Test`註解。
向測試類新增一個帶有`@BeforeMethod`註解的方法`beforeMethod`。
實現測試條件並檢查`@BeforeMethod`註解的行為。
以下是`TestAnnotationBeforeMethod.java`的內容:
import org.testng.Assert; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; public class TestAnnotationBeforeMethod { @BeforeMethod public void beforeMethod(){ System.out.println("executing beforeMethod before each method"); } @Test public void testMethodOne(){ Assert.assertEquals("Test method one", (new MessageUtil("executing testMethodOne method")).printMessage()); } @Test public void testMethodTwo(){ Assert.assertEquals("Test method two", (new MessageUtil("executing testMethodTwo method")).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="TestAnnotationBeforeMethod"/> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
使用javac編譯測試用例。
/work/testng/src$ javac TestAnnotationBeforeMethod.java MessageUtil.java
現在,執行`testng.xml`,這將執行在`
/work/testng/src$ java org.testng.TestNG testng.xml
驗證輸出。
executing beforeMethod before each method executing testMethodOne method executing beforeMethod before each method executing testMethodTwo method =============================================== Suite Total tests run: 2, Passes: 0, Failures: 2, Skips: 0 ===============================================