- 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 retrieve test method description in TestNG before and after execution?
TestNG supports native dependency injection. It allows to declare additional parameters in methods. At runtime, TestNG automatically fills these parameters with the correct values. Following is a set of native dependencies in TestNG −
- ITestContext
- XmlTest
- Method
- ITestResult
These dependencies help to retrieve the description of Test method, if written. A Test method name can be retrieved before or after the execution of the test.
If the user wants to get the description of a test method prior to its execution, then @BeforeMethod can be useful to retrieve it.
If the user wants to know the description of a Test method after it is executed, then @AfterMethod can be used.
The actual code can be written in either of these methods to retrieve the Test method description. @BeforeMethod and @AfterMethod supports all these native dependencies. The full access of these dependencies is given in the following table −
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 (in @BeforeMethod) and ITestResult (in @AfterMethod) to show how to retrieve the description of a Test method. However, any of these dependencies can be used for @BeforeMethod or @AfterMethod. The only change will be in import part where the corresponding library should be imported as per used native dependency.
Scenario 1
Suppose the user wants to retrieve the description of a Test method before the execution. In this case, the code will be written inside @BeforeMethod to retrieve the Test method description.
As @BeforeMethod executes each time before the @Test method, the Test method name and description will be printed before execution and after that, the Test method will be executed. In this scenario, we will implement Method dependency.
Approach/Algorithm to solve this problem
Step 1 − Create a TestNG class - OrderofTestExecutionInTestNG and write @BeforeMethod method.
Step 2 − Write the following code inside @BeforeMethod−
public void name(Method method) { System.out.println("Test name is " + method.getName()); System.out.println("Test description is " + method.getAnnotation(Test.class).description()); }
Step 3 − Write two different @Test methods in the class - NewTestngClass.
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 - OrderofTestExecutionInTestNG
src/ OrderofTestExecutionInTestNG.java
import org.testng.annotations.*; import java.lang.reflect.Method; public class OrderofTestExecutionInTestNG { // test case 1 @Test(description="test case 1 to execute") public void testCase1() { System.out.println("in test case 1 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(description="test case 2 to execute") public void testCase2() { System.out.println("in test case 2 of OrderofTestExecutionInTestNG"); } @BeforeMethod() public void name(Method method) { System.out.println("Test name is: " + method.getName()); System.out.println("Test description is: " + method.getAnnotation(Test.class).description()); } }
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 = " OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
Output
Test name is: testCase1 Test description is: test case 1 to execute in test case 1 of OrderofTestExecutionInTestNG Test name is: testCase2 Test description is: test case 2 to execute in test case 2 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
Scenario 2
When the user wants to retrieve the description of test method after the execution. In this case, code will be written inside @AfterMethod to retrieve the test method description.
As @AfterMethod executes each time after the @Test method, test method name and description will be printed after execution of Test method. In this scenario, we will implement ITestResult dependency.
Approach/Algorithm to solve this problem:
Step 1 − Create a TestNG class - OrderofTestExecutionInTestNG and write @BeforeMethod method.
Step 2 − Write the following code inside @BeforeMethod −
public void name(ITestResult result){ System.out.println("Test Name is: " +result.getMethod().getMethodName()); System.out.println("Test description is: "+result.getMethod().getDescription()); }
Step 3 − Write 2 different @Test method in the class - OrderofTestExecutionInTestNG.
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 - OrderofTestExecutionInTestNG: src/ OrderofTestExecutionInTestNG.java
import org.testng.annotations.*; import org.testng.ITestResult; public class OrderofTestExecutionInTestNG { // test case 1 @Test(description="test case 1 to execute") public void testCase1() { System.out.println("in test case 1 of OrderofTestExecutionInTestNG"); } // test case 2 @Test(description="test case 2 to execute") public void testCase2() { System.out.println("in test case 2 of OrderofTestExecutionInTestNG"); } @AfterMethod public void name(ITestResult result){ System.out.println("Test Name is:" +result.getMethod().getMethodName()); System.out.println("Test description is:" +result.getMethod().getDescription()); } }
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 = " OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
Output
in test case 1 of OrderofTestExecutionInTestNG Test Name is:testCase1 Test description is:test case 1 to execute in test case 2 of OrderofTestExecutionInTestNG Test Name is:testCase2 Test description is:test case 2 to execute =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
- Related Articles
- How to retrieve test method name in TestNG before and after execution?
- How to get the test group name in TestNG before and after execution?
- How to overlook a particular test method from execution in TestNG?
- How does the execution of a particular test method depend on other test\nmethods in TestNG?
- How to retrieve all test methods name in TestNG suite?
- How to retrieve the test suite name at runtime in TestNG?
- How to incorporate and remove test methods from execution from a\ncollection of test cases in TestNG?
- How to skip a particular test method in TestNG?
- How to achieve parallel execution in TestNG?
- What is the order of test execution with priority in TestNG?
- How does running a specific test method depend on another test method in TestNG?
- How to perform parameterization in execution in TestNG?
- How to skip a particular test method from execution in Cucumber?
- How to run test groups in TestNG?
- How will you run a prerequisite method and post condition method for\nevery test in TestNG?
