How to get the test result status from TestNG/Selenium in @AfterMethod?

35,557

Solution 1

just do it:

public class stacktest  {


@Test
public void teststackquestion() {

    boolean actual = true;
    boolean expected = false;
   Assert.assertEquals(actual, expected);

}


@AfterMethod
public void afterMethod(ITestResult result)
{
    try
 {
    if(result.getStatus() == ITestResult.SUCCESS)
    {

        //Do something here
        System.out.println("passed **********");
    }

    else if(result.getStatus() == ITestResult.FAILURE)
    {
         //Do something here
        System.out.println("Failed ***********");

    }

     else if(result.getStatus() == ITestResult.SKIP ){

        System.out.println("Skiped***********");

    }
}
   catch(Exception e)
   {
     e.printStackTrace();
   }

}

}

Solution 2

The TestListenerAdapter has methods for each of those situations (success, skipped, failure). My suggestions is to make your own listener like this.

public class MyTestResultListener extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult result) {
        // do what you want to do
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        // do what you want to do
    }

   @Override
    public void onTestSkipped(ITestResult result) {
        // do what you want to do
    }
}

Then add your listener to the test class.

@Listeners(MyTestResultListener.class)
public class MyTest {

// your tests

}
Share:
35,557
hirosht
Author by

hirosht

C#

Updated on July 09, 2022

Comments

  • hirosht
    hirosht almost 2 years

    For a research I'm doing, I'm in need of capturing the result status (Passed/Failed) after running the test method (@Test), from @AfterMethod.

    I have been using the import org.testng.ITestResult; as an out come of my research to get my work easier after going the several online blogs, but It seems like it didn't success my expectation as always the result outputs as passed, even though an assertion failed.

    My Code is as follows :

    public class SampleTestForTestProject {
    ITestResult result;
    
    @Test(priority = 1)
    public void testcase(){
    
        // intentionally failing the assertion to make the test method fail 
        boolean actual = true;
        boolean expected = false;
        Assert.assertEquals(actual, expected);
    
    }
    
    @AfterMethod
    public void afterMethod()  {
    
        result = Reporter.getCurrentTestResult();
    
        switch (result.getStatus()) {
        case ITestResult.SUCCESS:
            System.out.println("======PASS=====");
            // my expected functionality here when passed
            break;
    
        case ITestResult.FAILURE:
            System.out.println("======FAIL=====");
            // my expected functionality here when passed
            break;
    
        case ITestResult.SKIP:
            System.out.println("======SKIP BLOCKED=====");
            // my expected functionality here when passed
            break;
    
        default:
            throw new RuntimeException("Invalid status");
        }
      }
    }
    

    Result in the Console :

    [TestNG] Running:  C:\Users\USER\AppData\Local\Temp\testng-eclipse--988445809\testng-customsuite.xml
    
    ======PASS=====
    
    FAILED: testcaseFail
    java.lang.AssertionError: expected [false] but found [true]
    

    My expectation is to get the test result to a variable to get through the switch, as given in the above code snippet, and get printed "======FAIL=====" when the test method fail.

    Will someone be able assist me kindly to catch the execution test result for each test method (@Test). If the method I have approached is wrong, please assist me with a code snippet to the correct approach, kindly.

    Thank you in advance

  • hirosht
    hirosht about 8 years
    May I kindly know the reason for suggestion a nested if-else rather than a switch in your answer? Anyhow i tried your solution, which still gives the result as PASSED though the assertion is failing the test as shown in above "Result in the Console"
  • noor
    noor about 8 years
    no reason, i use like this that's why i wrote like this....u can use switch instead of if else....no problem.
  • hirosht
    hirosht about 8 years
    did it gave you the accurate test result somehow? I wonder whether i confused you with my question. I am not getting the accurate test result and it always gives the test result as PASSED, even though the test is actually FAILED. Will you be able to kindly assist me further?
  • noor
    noor about 8 years
    the solution i give u is tested ..... if u goole about this, u will see such kind of solution every where, so i think problem is in ur code....please debug ur code...i think u can than find ur problem.
  • noor
    noor about 8 years
    ur problem is in here- Assert.assertEquals(actual, expected);try to use this- Assert.assertNotEquals(actual1, actual2); as the first assertion looking for two equal boolean and thats why it throws an exception.
  • hirosht
    hirosht about 8 years
    I hope Im not troubling you. I intentionally used the Assert.assertEquals(X, Y); instead of Assert.assertNotEquals(X, Y) to FAIL the testMethod. That is to see if the FAIL is getting printed in console. But it didn't printed what i expected, as you can see. I need to get the fail printed when its failing the test case / test method. Please assist me?
  • noor
    noor about 8 years
    actually, If the two objects are equal according to their implementation of their equals() method, the assertEquals() method will return normally. Otherwise the assertEquals() method will throw an exception, and the test will stop there. As u r comparing two non equal boolean in assertEquals() method, thats why it shows an exception.
  • hirosht
    hirosht about 8 years
    Precisely. My concern is about the switch i have written. It wont get throught the if(result.getStatus() == ITestResult.FAILURE) as result.getStatus() = ITestResult.SUCCESS, where it should be ITestResult.FAILURE. You have any idea, why the result.getresult() is not returning a FAILURE?
  • noor
    noor about 8 years
    i have updated my code, i run this on my machine and it prints the failed message. i think it will also on ur machine.
  • hirosht
    hirosht about 8 years
    Yes @Noor. Thank you very much. I figured out where i have made the mistake. Reporter is not working in @AfterMethod. Once i passed the ITestResult result as an argument to the after-method instead, it gave me a successfully expected outcome. Thank you very much for your code snippet and for assisting me to seek my solution with patience.
  • bnieland
    bnieland over 7 years
    I have found that a test listener is more reliable than @afterMetod. Using @afterMethod was not executing for failed tests in some cases.
  • David Baak
    David Baak over 7 years
    @bnieland exactly, that is why I suggested it. But it wasn't selected as an answer nor voted up. So I guess it wasn't what he was looking for.
  • hirosht
    hirosht over 6 years
    @DavidBaak: Well the customized listner was the next approach I tried and which worked out a a success for me! Thank you. Upvoted & Chosen!
  • David Baak
    David Baak over 6 years
    @hirosht Thanks. Could you mark my response as an answer?
  • MansoorShaikh
    MansoorShaikh over 3 years
    @bnieland That would be resolved by setting alwaysRun=true in AfterMethod attribute
  • David Baak
    David Baak over 3 years
    @MansoorShaikh true, but the TestListenerAdapter is meant to be used for this type of situation. It's the cleaner solution too.