Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 535
hirosht Avatar asked Mar 21 '16 07:03

hirosht


People also ask

What is ITestResult in TestNG?

If you are not aware of TestNG and Selenium, we recommend you check our TestNG tutorial to run your first automation script. For every ITestListener method we usually pass the following arguments: “ITestResult” interface along with its instance “result” which describes the result of a test.

What is ITestListener in TestNG?

ITestListener: This is the most frequently used TestNG listener. ITestListener is an interface implemented in the class , and that class overrides the ITestListener defined methods. The ITestListener listens to the desired events and executes the methods accordingly.


2 Answers

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();
   }

}

}

like image 59
noor Avatar answered Sep 28 '22 04:09

noor


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

}
like image 23
David Baak Avatar answered Sep 28 '22 05:09

David Baak