- 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 the test suite name at runtime in TestNG?
TestNG supports native dependency injection. It allows to declare additional parameters in methods. At the runtime, TestNG automatically fills these parameters with the right value.
Here is a list of some of the native dependencies in TestNG −
- ITestContext
- XmlTest
- Method
- ITestResult
These dependencies help to retrieve the test suite name depending on where these are called. If the user wants to retrieve the test suite name before execution or after execution, the best place is @BeforeSuite or @AfterSuite.
@BeforeSuite and @AfterSuite support ITestContext. However, 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 ITestContext dependency to show how to retrieve test suite name.
Scenario 1
Suppose the user wants to retrieve the name of a test suite before the execution. In this case, the code will be written inside @BeforeSuite to retrieve suite name that will be executed.
As @BeforeSuite executes at first, the suite name will be printed before the execution of any test methods.
Approach/Algorithm to solve this problem
Step 1 − Create a TestNG class called NewTestngClass.
Step 2 − Write the following code inside @BeforeSuite in the class;
public void name(ITestContext context) { System.out.println("in beforesuite of NewTestngClass"); System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().getName()); }
Step 3 − Write two different @Test methods inside NewTestngClass.
Step 4 − Now create the testNG.xml as given below to run the TestNG classes.
Step 5 − 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.ITestContext; import org.testng.annotations.*; public class NewTestngClass { // test case 1 @Test() public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } // test case 2 @Test() public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } @BeforeSuite public void name(ITestContext context) { System.out.println("in beforesuite of NewTestngClass"); System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().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" parallel = "none"> <test name = "test1" preserve-order = "true"> <classes> <class name = "NewTestngClass"/> </classes> </test> </suite>
Output
in beforesuite of NewTestngClass Test Suite name is:Suite1 in test case 1 of NewTestngClass in test case 2 of NewTestngClass =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================
Scenario 2
Suppose the user wants to retrieve the name of the suite after the execution. In this case, the code will be written inside @AfterSuite to retrieve the suite name that is executed, as @AfterSuite executes once at last after the execution is completed.
Approach/Algorithm to solve this problem :
Step 1 − Create a TestNG class called NewTestngClass.
Step 2 − Write the following code inside @AfterSuite in the class;
public void name(ITestContext context) { System.out.println("in beforesuite of NewTestngClass"); System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().getName()); }
Step 3 − Write two different @Test methods inside NewTestngClass.
Step 4 − Create the testNG.xml as given below to run the TestNG classes.
Step 5 − 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.ITestContext; import org.testng.annotations.*; public class NewTestngClass { // test case 1 @Test() public void testCase1() { System.out.println("in test case 1 of NewTestngClass"); } // test case 2 @Test() public void testCase2() { System.out.println("in test case 2 of NewTestngClass"); } @AfterSuite public void name(ITestContext context) { System.out.println("in aftersuite of NewTestngClass"); System.out.println("Test Suite name is:"+context.getCurrentXmlTest().getSuite().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" parallel = "none"> <test name = "test1" preserve-order = "true"> <classes> <class name = "NewTestngClass"/> </classes> </test> </suite>
Output
in test case 1 of NewTestngClass in test case 2 of NewTestngClass in aftersuite of NewTestngClass Test Suite name is:Suite1 =============================================== Suite1 Total tests run: 2, Passes: 2, Failures: 0, Skips: 0 ===============================================