簡體   English   中英

TestNG 如何在測試失敗時執行 beforeclass() 方法

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

我在beforeclass()中有登錄步驟,之后測試開始執行。 如果測試失敗,則應該重新調用beforeclass()方法。 我正在使用測試監聽器 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          
    }
      
}

@AfterMethod可以知道測試的結果,如果失敗了,可以重復 after 方法中的邏輯。 在這種情況下不需要測試監聽器。

@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