- 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 current status of test methods in TestNG?
TestNG supports native dependency injection. It allows to declare additional parameters in methods. At the run time, TestNG automatically fill these parameters with right value. Following are few native dependencies in TestNG:
ITestContext
XmlTest
Method
ITestResult
These dependencies help to retrieve the test execution status.
Usually, @AfterMethod supports all these native dependencies and test status could be either Success, failure or Skip.
However, TestNG supports following test status those can be retrieved by calling the function at 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 ITestResult dependency to show how to retrieve test status.
When user wants to retrieve the status of test method after the execution. In this case, code will be written inside @AfterMethod to retrieve the test method status.
As @AfterMethod executes each time after the @Test method, status of executed test method name will be printed. Here, we will us getStatus() method and it returns integer as shown in above table. User should implement the String by using these status code as integer.
Approach/Algorithm to solve this problem
Step 1: Create a TestNG class − NewTestngClass and write @AfterMethod method.
Step 2: Write the following code inside @AfterMethod.
Step 3: Write 3 different @Test method in the class − NewTestngClass. One for Success, One for failure and One for skip as shown in program code.
Step 4: Now create the testNG.xml as given below to run the TestNG classes.
Step 5: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.
Example
The following code for 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 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 ===============================================