How do I run classes with TestNG by 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.

Following are few ways those are handy to use regular expression in a test class run from a test suite.

  • Mention all class names inside <classes>. And, inside the class use <methods> and <include name =”test.*” />. It will exclude all tests starting with name as test from the given class.

  • If classes are present in nested or different packages then use the package name until common name in <packages><package name=”common_package.*”> along with regular expression. It will run all classes present inside the package name starting as common_package.

In this article, we are going to discuss about #1 and #2.

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 few tests based on regular expression. We will run all @Test those names are starting as “test.”

Approach/Algorithm to solve this problem

  • Step 1: Create a TestNG class − NewTestngClass.

  • Step 2: Write 3 different @Test method in the class − NewTestngClass stating name as test1, test2 and findingAddition.

  • 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");
    }
    @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 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 and inside that multiple classes with 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 packages starting as com.

Approach/Algorithm to solve this problem

  • Step 1: Crete 2 packages under src as com.test and com.work

  • Step 2: Create 2 TestNG classes − NewTestngClass inside package com.test and OrderofTestExecutionInTestNG inside package com.work

  • Step 3: Write 2 different @Test method in both of the classes −stating name as test1 and test2.

  • 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/ 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 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
=======================

Updated on: 16-Aug-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements