如何在 TestNG 中為每個測試方法執行一個先決條件方法和一個後置條件方法?


利用 @BeforeMethod@AfterMethod 註解,我們可以在 TestNG 中為每個測試方法執行一個先決條件方法和一個後置條件方法。

示例

@BeforeMethod
public void prerequisite(){
   System.out.println("Run before every tests");
}
@AfterMethod
public void postcondition(){
   System.out.println("Run after every tests ");
}
@Test
public void loanPay(){
   System.out.println("Loan pay is successful");
}

在 Java 類檔案中,帶有 @BeforeMethod 的 prerequisite() 方法將作為每個測試方法的先決條件執行。然後執行 loanPay(),最後帶有 @AfterMethod 的 postcondition() 方法將執行。

更新於: 2020 年 6 月 11 日

265 次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告