
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Execute Single Test from a Large Testing Suite Using TestNG XML
testNG.xml is very flexible and it can work as a harness file to execute the test cases. It keeps the development and execution separate from each other. A user can develop "N" number of test cases in testNG but can run a limited number of test methods based on the configuration in testNG.xml.
In this article, let's see how to run only one test method from a large TestNG suite.
To run only one test method, we will use the 'include' keyword from TestNG. In testNG.xml, first we will define the class name where the method is present and after that mention the test name which is going to execute along with the 'include' keyword.
Approach/Algorithm to solve this problem −
Step 1 − import org.testng.annotations.* for TestNG in a class.
Step 2 − Write an annotation as @test
Step 3 − Create a method for the @test annotation as test1.
Step 4 − Repeat the steps for test2 and test3.
Step 5 − Similarly, create "n" number of classes and add multiple tests. In this example, we will create 2 classes with each class having 2 test methods.
Step 6 − Now create the testNG.xml as given below.
Step 7 − Run the testNG.xml or run the testNG class directly in IDE or compile and run it using command line.
Example
The following code shows how to run only 1 test method from a large suite −
src/ NewTestngClass.java
import org.testng.annotations.Test; 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"); } }
src/OrderofTestExecutionInTestNG.java:
import org.testng.annotations.Test; public class OrderofTestExecutionInTestNG { // test case 1 @Test public void testCase3() { System.out.println("in test case 3 of OrderofTestExecutionInTestNG"); } // test case 2 @Test public void testCase4() { System.out.println("in test case 4 of OrderofTestExecutionInTestNG"); } }
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"> <methods> <include name="testCase1" /> </methods> </class> </classes> </test> </suite>
Output
in test case 1 of NewTestngClass =============================================== Suite1 Total tests run: 1, Passes: 1, Failures: 0, Skips: 0