Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 974
Abhishek Thomas Avatar asked Aug 02 '18 06:08

Abhishek Thomas


1 Answers

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");
   }
}
like image 194
Mureinik Avatar answered Nov 16 '22 13:11

Mureinik