- 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 a list of all the test methods in a TestNG class?
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
These dependencies help to retrieve the names of test methods depending on where they are called. If the user wants to retrieve the names of all the test methods that are going to be executed in a class, the best place is @BeforeClass or @AfterClass. @BeforeClass and @AfterClass support ITestContext and XmlTest.
The following table shows the full access of these dependencies −
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 XmlTest dependency to show how to retrieve the names of all the test methods in a class.
Scenario 1
Suppose the user wants to retrieve the names of all the test methods present in a class before the execution. In this case, the code will be written inside @BeforeClass to retrieve the names of all the test methods that will be executed inside the class.
Since @BeforeClass executes each time before a new class starts executing in a test suite, the names of all the test methods will be printed before the execution of test methods in each class.
Approach/Algorithm to solve this problem −
Step 1 − Create two TestNG classes: NewTestngClass and OrderofTestExecutionInTestNG.
Step 2 − Write the following code inside @BeforeClass in both the classes;
public void name(ITestContext context) { System.out.println("in beforeclass of <class name>"); ITestContext[] a = context.getAllTestMethods(); for (ITestContext b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } }
Note: getAllTestMethods() gets the names of all the test methods as object, while getName() fetches the names individually.
Step 3 − Write two different @Test methods in both the classes.
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.annotations.BeforeClass; import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; 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"); } @BeforeClass public void name(ITestContext context) { System.out.println("in beforeClass of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
src/OrderofTestExecutionInTestNG.java −
import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.annotations.BeforeClass; public class OrderofTestExecutionInTestNG { // test case 3 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 4 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } @BeforeClass public void name(ITestContext context) { System.out.println("in beforeClass of OrderofTestExecutionInTestNG"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().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 = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
Output
in beforeClass of NewTestngClass testCase1 testCase2 in test case 1 of NewTestngClass in test case 2 of NewTestngClass in beforeClass of OrderofTestExecutionInTestNG testCase3 testCase4 in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================
Scenario 2
Suppose the user wants to retrieve the names of all the test methods present in a class after the execution. In this case, the code will be written inside @AfterClass to retrieve the names of all the test methods that are executed inside the class.
As @AfterClass executes each time after a new class completes execution in a test suite, the names of all the test methods will be printed after the execution of test methods in each class.
Approach/Algorithm to solve this problem −
Step 1 − Create two TestNG classes as NewTestngClass and OrderofTestExecutionInTestNG.
Step 2 − Write the following code inside @AfterClass in both the classes −
public void name(ITestContext context) { System.out.println("in beforeclass of "); ITestContext[] a = context.getAllTestMethods(); for (ITestContext b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } }
Note − getAllTestMethods() gets the names of all the test methods as an object, while getName() fetches the names individually.
Step 3 − Write two different @Test methods in both the classes
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.annotations.AfterClass; import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; 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"); } @AfterClass public void name(ITestContext context) { System.out.println("in AfterClass of NewTestngClass"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().getName()); } } } }
src/OrderofTestExecutionInTestNG.java −
import org.testng.annotations.Test; import org.testng.ITestContext; import org.testng.ITestNGMethod; import org.testng.annotations.BeforeClass; public class OrderofTestExecutionInTestNG { // test case 3 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 4 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } @AfterClass public void name(ITestContext context) { System.out.println("in AfterClass of OrderofTestExecutionInTestNG"); ITestNGMethod[] a = context.getAllTestMethods(); for (ITestNGMethod b : a) { if (b.getRealClass() == this.getClass()) { System.out.println(b.getConstructorOrMethod().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"/> <class name = "OrderofTestExecutionInTestNG"/> </classes> </test> </suite>
Output
in test case 1 of NewTestngClass in test case 2 of NewTestngClass in AfterClass of NewTestngClass testCase1 testCase2 in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG in AfterClass of OrderofTestExecutionInTestNG testCase3 testCase4 =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 ===============================================
- Related Articles
- How to retrieve all test methods name in TestNG suite?
- How do I get list of methods in a Python class?
- How to incorporate and remove test methods from execution from a\ncollection of test cases in TestNG?
- How can a particular group of test cases get executed in TestNG?
- How to get the name of a test method that was run in a TestNG teardown method?
- Get the list of all the declared methods in Java
- Get the list of all the public methods in Java
- How do I get a list of all instances of a given class in Python?
- How to skip a particular test method in TestNG?
- How to get the test group name in TestNG before and after execution?
- How to disable a TestNG test based on a condition?
- List methods of a class using Java Reflection
- How to run test groups in TestNG?
- How does the execution of a particular test method depend on other test\nmethods in TestNG?
- How to overlook a particular test method from execution in TestNG?
