- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the result status from TestNG in @AfterMethod?
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.
org.testng.ITestResult | ||
---|---|---|
public static final int | FAILURE | 2 |
public static final int | SKIP | 3 |
public static final int | STARTED | 16 |
public static final int | SUCCESS | 1 |
public static final int | SUCCESS_PERCENTAGE_FAILURE | 4 |
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.
Example
Use the following code for the common TestNG class "NewTestngClass" −
src/ NewTestngClass.java
import org.testng.ITestResult; import org.testng.SkipException; import org.testng.annotations.*; public class NewTestngClass { @Test() public void testcase1(){ System.out.println("TestCase 1 - Execution started"); assert true; } @Test() public void testcase2(){ System.out.println("TestCase 2 - Execution started"); assert false; } @Test() public void testcase3(){ System.out.println("TestCase 3 - Execution started"); throw new SkipException("Skipping the testcase 3"); } @AfterMethod() public void status(ITestResult result){ System.out.println("Status of execution is:"+result.getStatus()); try{ 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){ e.printStackTrace(); } } }
testNG.xml
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 "http://testng.org/testng-1.0.dtd" > <suite name = "Suite1"> <test name = "test1"> <classes> <class name = "NewTestngClass"/> </classes> </test> </suite>
Output
TestCase 1 - Execution started Status of execution is:1 Test case execution status is SUCCESS TestCase 2 - Execution started java.lang.AssertionError at NewTestngClass.testcase2(NewTestngClass.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133) at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824) at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.util.ArrayList.forEach(ArrayList.java:1259) at org.testng.TestRunner.privateRun(TestRunner.java:794) at org.testng.TestRunner.run(TestRunner.java:596) at org.testng.SuiteRunner.runTest(SuiteRunner.java:377) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332) at org.testng.SuiteRunner.run(SuiteRunner.java:276) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212) at org.testng.TestNG.runSuitesLocally(TestNG.java:1134) at org.testng.TestNG.runSuites(TestNG.java:1063) at org.testng.TestNG.run(TestNG.java:1031) at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66) at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109) 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 =============================================== Suite1 Total tests run: 3, Passes: 1, Failures: 1, Skips: 1 ===============================================