简体   繁体   中英

Run @AfterMethod only when my @Test is passed, not if @Test fails

My afterMethod consists of this:

@AfterMethod(groups = { "Regression" })
public void afterMethod() {
    // Setting driver used to false as this test case is pass
    driverUsed.put("supportDriver", false);
    System.out.println("Test case is pass");
}

where I am putting the driver false

But I want to run this afterMethod only when my @Test is passed, not when @test fails.

To quote TestNG's documentation :

Any @AfterMethod method can declare a parameter of type ITestResult , which will reflect the result of the test method that was just run.

You can use this parameter to check if the test succeeded or not:

@AfterMethod(groups = { "Regression" })
public void afterMethod(ITestResult result) {
   if (result.getStatus() == ITestResult.SUCCESS) {
        // Setting driver used to false as this test case is pass
        driverUsed.put("supportDriver", false);
        System.out.println("Test case is pass");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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