VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/how-to-use-regex-in-testng/

⇱ How to use Regex in TestNG? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to use Regex in TestNG?

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
  • 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.

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

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.

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

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.


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


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:


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:

Output:

👁 Screenshot-(328)
Output
Comment
Article Tags:
Article Tags:

Explore