How do I Run Classes with TestNG by Name and Wildcard in testng.xml?


testng.xml has a format as <classes> where we define what all test classes should be executed. There is no any specific way to provide regular expression in a class in <classes>. But there are work arounds those are useful to run specific @Test from a class. TestNG supports regular expression at include, exclude and package tags.

Here, the problem statement is when a user wants to run only specific classes those are having specific format like initial name of classes should be same. For example, the user wants to run all classes whose name starts with NewTest.

In this tutorial, we are going to discuss how to run all classes whose name starts with NewTest.

The solution of above problem could be in beanshell scripting. User can provide a simple code inside testng.xml using tag <method− selectors> instead of <classes>. It will evaluate at run time and fetch class name those should run.

Approach/Algorithm to Solve this Problem

  • Step 1: Create 3 TestNG classes − NewTestngClass, NewTestNGClass1 and OrderofTestExecutionInTestNG.

  • Step 2: Write a @Test method in all the classes.

  • Step 3: Now create the testNG.xml as given below.

  • Step 4: Now, run the testNG.xml or directly testNG class in IDE or compile and run it using command line.

Example

The following code to show 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");
    }
    
}  

src/ NewTestngClass1.java

import org.testng.annotations.Test;

public class NewTestngClass {

    @Test
    public void testCase1() {
        System.out.println("in test case 1 of NewTestngClass1");
    }
}  

src/ NewTestngClass.java

import org.testng.annotations.Test;

public class OrderofTestExecutionInTestNG {

    @Test
    public void testCase1() {
        System.out.println("in test case 1 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 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">
        <method-selectors>
    <method-selector>
        <script language="beanshell"><![CDATA[
            method.getDeclaringClass().getSimpleName().startsWith("NewTest")
               ]]>
		</script>
    </method-selector>
</method-selectors>
    </test>
</suite>

Output

in test case 1 of NewTestngClass
in test case 1 of NewTestngClass1

===============================================
Suite1
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

Updated on: 21-Aug-2023

123 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements