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

TestNGRest AssuredDynamic Programming

TestNG supports native dependency injection. It allows to declare additional parameters in methods. At the runtime, TestNG automatically fills these parameters with the right values. Here is a list of some native dependencies in TestNG:

  • ITestContext

  • XmlTest

  • Method

  • ITestResult

You can use these dependencies to get the execution status of a test in TestNG.

Usually, @AfterMethod supports all these native dependencies and the test status could be either Success, Failure or Skip.

TestNG supports the following test status that can be retrieved by calling the function at the right place.

public static final int
public static final int
public static final int
public static final int
public static final int

In this article, we will use the ITestResult dependency to show how to retrieve the result status of a test in TestNG.

Suppose a user wants to retrieve the status of a test method after the execution. In this case, the code will be written inside @AfterMethod to retrieve the test method status.

As @AfterMethod executes each time after the @Test method, we will have the status of the executed test method name displayed each test. Here, we will use the getStatus() method and it will return an integer as shown in the above table. The user should implement the String by using these status codes as integer.

Approach/Algorithm to solve this problem −

  • Step 1 − Create a TestNG class called NewTestngClass and write the @AfterMethod method.

  • Step 2 − Write 3 different @Test methods in the class, one for Success, one for Failure, and one for Skip, as shown in the program code.

  • Step 3 − Now create the testNG.xml as given below to run the TestNG classes.

  • Step 4 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.


Use the following code for the common TestNG class "NewTestngClass" −


import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.*;
public class NewTestngClass {
   public void testcase1(){
      System.out.println("TestCase 1 - Execution started");
      assert true;
   public void testcase2(){
      System.out.println("TestCase 2 - Execution started");
      assert false;
   public void testcase3(){
      System.out.println("TestCase 3 - Execution started");
      throw new SkipException("Skipping the testcase 3");
   public void status(ITestResult result){
      System.out.println("Status of execution is:"+result.getStatus());
         if(result.getStatus() == ITestResult.SUCCESS){
            System.out.println("Test case execution status is SUCCESS");
         else if(result.getStatus() == ITestResult.FAILURE){
            // Do something here
            System.out.println("Test case execution status is FAILURE");
         else if(result.getStatus() == ITestResult.SKIP ){
            System.out.println("Test case execution status is SKIP");
      catch(Exception e){


This is a configuration file that is used to organize and run the TestNG test cases. It is very handy when limited tests are needed to execute rather than the full suite.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "" >
<suite name = "Suite1">
   <test name = "test1">
         <class name = "NewTestngClass"/>


TestCase 1 - Execution started
Status of execution is:1
Test case execution status is SUCCESS
TestCase 2 - Execution started

   at NewTestngClass.testcase2(
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(
   at java.lang.reflect.Method.invoke(
   at org.testng.internal.MethodInvocationHelper.invokeMethod(
   at org.testng.internal.TestInvoker.invokeMethod(
   at org.testng.internal.TestInvoker.invokeTestMethod(
   at org.testng.internal.MethodRunner.runInSequence(
   at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(
   at org.testng.internal.TestInvoker.invokeTestMethods(
   at org.testng.internal.TestMethodWorker.invokeTestMethods(
   at java.util.ArrayList.forEach(
   at org.testng.TestRunner.privateRun(
   at org.testng.SuiteRunner.runTest(
   at org.testng.SuiteRunner.runSequentially(
   at org.testng.SuiteRunner.privateRun(
   at org.testng.SuiteRunnerWorker.runSuite(
   at org.testng.TestNG.runSuitesSequentially(
   at org.testng.TestNG.runSuitesLocally(
   at org.testng.TestNG.runSuites(
   at com.intellij.rt.testng.RemoteTestNGStarter.main(
Status of execution is:2
Test case execution status is FAILURE
TestCase 3 - Execution started

Test ignored.
Status of execution is:3
Test case execution status is SKIP

Total tests run: 3, Passes: 1, Failures: 1, Skips: 1
Updated on 09-Mar-2022 11:40:01