[英]TestNG Annotations order
我對 TestNG 注釋的操作順序有疑問......我有以下代碼:
public class AnnotationsTest {
@BeforeSuite(alwaysRun = true)
public static void beforeSuite() {
System.out.println("@BeforeSuite");
}
@BeforeClass(alwaysRun = true)
public static void beforeClass() {
System.out.println("@BeforeClass");
}
@BeforeTest(alwaysRun = true)
public static void beforeTest() {
System.out.println("@BeforeTest");
}
@BeforeMethod(alwaysRun = true)
public static void beforeMethod() {
System.out.println("@BeforeMethod");
}
@AfterSuite(alwaysRun = true)
public static void afterSuite() {
System.out.println("@AfterSuite");
}
@AfterClass(alwaysRun = true)
public static void afterClass() {
System.out.println("@AfterClass");
}
@AfterTest(alwaysRun = true)
public static void afterTest() {
System.out.println("@AfterTest");
}
@AfterMethod(alwaysRun = true)
public static void afterMethod() {
System.out.println("@AfterMethod");
}
@Test
public void test() {
System.out.println("Test");
}
@Test
public void test2() {
System.out.println("Test2");
}
}
我的輸出如下:
我的問題是,為什么 @AfterTest 方法不在每個 @Test 注釋之后運行? TestNG 是否將整個班級視為“測試”? 似乎是這種情況,因為@BeforeTest 和@AfterTest 位於@BeforeClass 和@AfterClass 之外,但我想確保我理解。 我假設在這種情況下我可以使用 @BeforeMethod 和 @AfterMethod 在這個類中的 test1 和 test2 之前和之后執行。 謝謝!
我的問題是,為什么 @AfterTest 方法不在每個 @Test 注釋之后運行?
正如文檔所說
@AfterTest:注解的方法將在屬於 < test > 標簽內的類的所有測試方法運行后運行。
和
@BeforeTest:注釋的方法將在任何屬於 < test > 標簽內的類的測試方法運行之前運行。
這里的主要混淆是@Test 注釋和標簽。 考慮您正在從 testng.xml 文件執行上述代碼。 我們在該文件中編寫 testng 文件標記序列的方式是 methods 。 所以現在每個注釋都有意義了。 即@Test 用於該類中的方法。 @BeforeMethod 將在每個 @Test 注釋之前執行。 並且 @BeforeTest 注釋在 testng.xml 文件中提到的類之前執行,因為您可以看到標記包含標記。 因此,上述問題的簡短回答是 @Test 用於在每個 @Test 注釋方法之前執行 @BeforeMethod 並且 @BeforeTest 在標記中提到的所有方法和類之前執行。
@AfterMethod
是在@Test
方法之后執行的方法。 類似地, @BeforeMethod
是在每個@Test
方法之前執行的方法。
您的輸出顯示了正在運行的方法的優先級。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.