
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
Use Regular Expression in Class Path in TestNG XML
testNG.xml has a format <classes> where we define all the test classes that should be executed. There is no any specific way to provide regular expression in a class in <classes>. But there are workarounds of course which are quite useful in case you want to run a specific @Test from a class. TestNG supports regular expression at include, exclude and package tags.
Following are the ways in which you can use regular expression in a test class that is to be run from a test suite.
Mention all the class names inside <classes>. And, inside the class, use <methods> and <include name ="test.*" />. It will exclude all the tests starting with the name as test from the given class.
If classes are present in nested or different packages, then use the package name until the common name in <packages><package name="common_package.*"> along with the regular expression. It will run all the classes present inside the package name starting as common_package.
Now, let's discuss these two scenarios in detail.
Scenario 1
Use regular expression to run specific methods in a class. Here, we will have one class with multiple test methods, and we will see how testNG.xml is configured to run only a few tests based on regular expression. We will run all the @Test the names of which start as "test".
Approach/Algorithm to solve this problem −
Step 1 − Create a TestNG class called "NewTestngClass".
Step 2 − Write three different @Test methods in the class, with the starting names as test1, test2 and findingAddition.
Step 3 − Now create the testNG.xml as given below.
Step 4 − 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"); } @Test public void findingAddition() { System.out.println("in finding addition of NewTestngClass"); } }
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"> <methods> <include name="test.*"/> </methods> </class> </classes> </test> </suite>
Output
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
Use regular expression to run specific methods in a class. Here, we will have one package with multiple classes and multiple test methods, and we will see how testNG.xml is configured to run only specific packages based on regular expression. We will run the classes those are present in the packages with "com" as the starting name.
Approach/Algorithm to solve this problem −
Step 1 − Crete two packages under src as com.test and com.work
Step 2 − Create two TestNG classes: NewTestngClass inside the package com.test and OrderofTestExecutionInTestNG inside the package com.work
Step 3 − Write two different @Test methods in both the classes with the starting names as test1 and test2.
Step 4 − Now create the testNG.xml as given below.
Step 5 − 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/ com.test.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/com.work.OrderofTestExecutionInTestNG.java −
package com.test.exclude.class; 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"> <packages> <package name="com.*"> </package> </packages> </test> </suite>
Output
in test case 1 of NewTestngClass in test case 2 of NewTestngClass in test case 3 of OrderofTestExecutionInTestNG in test case 4 of OrderofTestExecutionInTestNG =============================================== Suite1 Total tests run: 4, Passes: 4, Failures: 0, Skips: 0 =======================