How to use Regex in TestNG? (original) (raw)

Last Updated : 1 Apr, 2024

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

Table of Content

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

Example 1: To Include Test Cases using Regex in TestNG

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

Java `

package com.geeksforgeeks.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 `

    </classes>
</test>

`

**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:

Screenshot-(324)

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

Example 2: To Exclude Test Case using Regex in TestNG

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

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test;

public class RegExclude { @Test public void cart () { System.out.println("Testing for Cart"); } @Test public void useraccount() { System.out.println("Testing for useraccount"); } @Test public void previousorders() { System.out.println("Testing for previousorders"); } @Test public void previousViewedItems() { System.out.println("Testing for previousViewedItems"); } @Test public void wishlist() { System.out.println("Testing for wishlist"); } }

`

**Explanation: In this Java Project, Inside the Regex Class, there are five methods named as cart(), useraccount(), previousorders(), previousViewedItems(), wishlist(). These methods are performing some task. Suppose we want to exclude that methods for test that starts with previous. For that we will use regular expression inside testng.xml file.

**Step 2: To exclude that methods for test that starts with previous word. For that we will use regular expression inside testng.xml file.

XML `

    </classes>
</test>

`

**Explanation: Inside this XML file we use exclude keyword inside methods and add "previous.*" regular expression. In the output we will observe that test cases start with previous word will not run.

**Step3: 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:

Screenshot-(325)

Output

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

Java `

package com.geeksforgeeks.test;

import org.testng.annotations.Test;

public class RegexGroup {

@Test(groups = {"high priority"})
public void testMethod1() {
    System.out.println("Testing for high priority");
}

@Test(groups = {"less priority"})
public void testMethod2() {
    System.out.println("Testing for less priority");
}

@Test(groups = {"high priority"})
public void testMethod3() {
    System.out.println("Testing for high priority group 2");
}

}

`

**Explanation: The class RegexGroup contains three test methods: testMethod1(), testMethod2(), and testMethod3(). Each test method is annotated with @Test, indicating that it's a test method. Two groups are defined for the tests using the groups attribute of the @Test annotation. testMethod1() and testMethod3() are assigned to the group "high priority". testMethod2() is assigned to the group "less priority".

**Step 2: Now, in the testng.xml file, you can specify a regular expression pattern to include specific groups:

XML `

`

**Explanation: This TestNG XML configuration excludes test methods in the "high priority" group from the test named "test1" within the "Test Suite", while including all other test methods from the RegexGroup class.

**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:

Screenshot-(327)

Output

Now in the testng.xml file, you can specify a regular expression pattern to exclude specific groups:

XML `

`

**Output:

Screenshot-(328)

Output