如何在 TestNG 中为每个测试运行先决条件方法和后置条件方法?
testngframeworkjava programming 更新于 2025/6/25 12:22:17
借助 @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() 方法将被运行。