- Trending Categories
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 name of a test method that was run in a TestNG teardown method?
TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Here's a set of native dependencies in TestNG:
- ITestContext
- XmlTest
- Method
- ITestResult
These dependencies help to retrieve the name of Test method. The name of a Test method can be retrieved before or after the execution of the test.
If the user wants to get the name of a Test method prior to its execution, then @BeforeMethod can be useful to retrieve it.
On the other hand, if the user wants to know which Test method is just executed, then @AfterMethod can be used. The actual code can be written in either of these methods to retrieve the name of a Test method.
@BeforeMethod and @AfterMethod support all these native dependencies. The full access of these dependencies is given below −
Annotation | ITestContext | XmlTest | Method | ITestResult |
---|---|---|---|---|
BeforeSuite | Yes | No | No | No |
BeforeTest | Yes | Yes | No | No |
BeforeGroups | Yes | Yes | No | No |
BeforeClass | Yes | Yes | No | No |
BeforeMethod | Yes | Yes | Yes | Yes |
Test | Yes | No | No | No |
AfterMethod | Yes | Yes | Yes | Yes |
AfterClass | Yes | Yes | No | No |
AfterGroups | Yes | Yes | No | No |
AfterTest | Yes | Yes | No | No |
AfterSuite | Yes | No | No | No |
In this article, we will use Method dependency to show how to retrieve the name of a Test method. However, any of these dependencies can be used for @BeforeMethod or @AfterMethod. The only change will be in the import part where the corresponding library should be imported as per the native dependency used.
Suppose the user wants to retrieve the name of a Test method after its execution. In this case, the code will be written inside @AfterMethod to retrieve the name of the Test method. As @AfterMethod executes each time after the @Test method, the name of the Test method will be printed after its execution.
Approach/Algorithm to solve this problem
Step 1 − Create a TestNG class NewTestngClass and write the @AfterMethod method.
Step 2 − Write the following code inside @AfterMethod
public void tearDown(Method method) { System.out.println("Test name: " + method.getName()); }
Note − Instead of parameter Method, any of remaining three native dependencies can be used. For example, ITestContext or XmlTest or ITestResult.
Step 3 − Write two different @Test methods in the class NewTestngClass.
Step 4 − Create the testNG.xml as given below to run the TestNG classes.
Step 5 − Finally, run the testNG.xml or directly testNG class 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.annotations.AfterMethod; import org.testng.annotations.Test; import java.lang.reflect.Method; public class NewTestngClass { @Test public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } @Test public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } @AfterMethod public void tearDown(Method method) { System.out.println("Test name: " + method.getName()); } }
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
in test case 1 of NewTestngClass Test name: testCase1 in test case 2 of NewTestngClass Test name: testCase2 =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
- Related Articles
- How will you run a prerequisite method and post condition method for\nevery test in TestNG?
- How to skip a particular test method in TestNG?
- How does running a specific test method depend on another test method in TestNG?
- How to retrieve test method name in TestNG before and after execution?
- How to run test groups in TestNG?
- How to overlook a particular test method from execution in TestNG?
- How to run multiple test classes in TestNG?
- How does the execution of a particular test method depend on other test\nmethods in TestNG?
- How does TestNG invoke a test method using multiple threads?
- How to specify method name sequence in TestNG?
- How to run multiple test cases using TestNG Test Suite in Selenium?
- How to get the test group name in TestNG before and after execution?
- How to get a list of all the test methods in a TestNG class?
- How to retrieve test method description in TestNG before and after execution?
- How to run a test method without reporting as passed or failed in pytest?
