Example 1: To Include Test Cases using Regex in TestNG

Step 1: First we will create a Java Project

Java
package com.w3wiki.test;

import org.testng.annotations.Test;

public class Regex {
  @Test
  public void signup() {
      System.out.println("Testing for signup");
  }
  @Test
  public void login() {
      System.out.println("Testing for login");
  }
  @Test
  public void signout() {
      System.out.println("Testing for signout");
  }
  @Test
  public void Order() {
      System.out.println("Testing for order");
  }
  @Test
  public void Payment() {
      System.out.println("Testing for payment");
  }
  
}

Explanation: In this Java Project, Inside the Regex Class, there are five methods named as signup(), login(), signout(), Order(), Payment(). These method are performing some task. Suppose we want to include only that method for test that starts with sign. For that we will use regular expression inside testng.xml file.

Step 2: To Include only that method for test that starts with sign. For that we will use regular expression inside testng.xml file.

XML
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
    <test name="test1">
        <classes>
               <class name="com.w3wiki.test.Regex">
                   <methods>
                        <include name="sign.*" />
                 </methods>
                 </class>
            
        </classes>
    </test>
</suite>

Explanation : Inside this XML file we use include keyword inside methods and add “sign.*” regular expression. In the output we will observe that only test case start with sign word will run.

Step 3: Run the testng.xml. Right click on the testng.xml file, move the cursor down to Run As and then click on the 1 TestNG Suite.

Output:

Output

Some Regular Expression:

“string.*” – The pattern /string .*/ searches for strings that start with the word “string” followed by a space character, and then followed by any number of characters denoted by the ‘*’ asterisk.

“*string.*” -The pattern .*string.* searches for strings that contain the word “string” anywhere within them. The .* before and after “string” allows for any sequence of characters (including none) to appear before and after the word “string”.

“.*string” – The pattern .*string” is a regular expression that matches strings ending with the sequence “string”. The .* indicates any sequence of characters (including none) that may appear before “string”, and the “, at the end, signifies the literal character ” followed by the end of the string

How to use Regex in TestNG?

In this article, we will see how to use Regex in TestNG. In TestNG, we can use Regular Expressions for two purposes:

  1. To Include Test Cases
  2. To Exclude Test Cases
  • To include test cases we use the include the keyword in the testng.xml configuration file.
  • To Exclude test cases we use the exclude keyword in the testng.xml configuration file.

Table of Content

  • Example 1: To Include Test Cases using Regex in TestNG
  • Example 2: To Exclude Test Case using Regex in TestNG
  • Example 3: In this example we will use regular expression in TestNG’s groups attribute in the @Test annotation

Let’s look at examples to understand how to use regular expressions to run specific test cases in TestNG.

Similar Reads

Example 1: To Include Test Cases using Regex in TestNG

Step 1: First we will create a Java Project...

Example 2: To Exclude Test Case using Regex in TestNG

Step 1: First we will create a Java Project...

Example 3: In this example we will use regular expression in TestNG’s groups attribute in the @Test annotation

Step 1: First we will create a Java Project...