简体   繁体   English

TestNG 如何在测试失败时执行 beforeclass() 方法

[英]TestNG How to execute beforeclass() method on testfailure

I have login steps in beforeclass() and after that tests starts executing.我在beforeclass()中有登录步骤,之后测试开始执行。 If there is a test failure, beforeclass() method should be reinvoked.如果测试失败,则应该重新调用beforeclass()方法。 I'm using test listener class.我正在使用测试监听器 class。

public class LoginTests {

    @BeforeClass
    public void beforeClass() throws Exception {
        System.out.print("login in application");        
    }    
      
    @BeforeMethod
    public void beforeMethod() {
            
    }     
      
    @AfterClass
    public void afterClass(){
      
    }     
      
    @Test
    public void test1() {
        System.out.print("go to first page");               
    }
    
    @Test
    public void test2() {
        System.out.print("go to second page");          
    }
          
    public void onTestFailure(ITestResult result) {       
        //if there is test failure run  beforeclass() method          
    }
      
}

You could know the result of the test in @AfterMethod , so if it is failed, then you could repeat the logic in the after method. @AfterMethod可以知道测试的结果,如果失败了,可以重复 after 方法中的逻辑。 No need for test listener in this case.在这种情况下不需要测试监听器。

@BeforeClass
public void beforeClass() throws Exception {
     //login
 }

@AfterMethod
public void afterMethod(ITestContext tc, ITestResult result) {
    if (result.isSuccess()) {
        return;
    }

    // login
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM