Example 2: To Exclude Test Case 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 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
<?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.RegExclude">
                   <methods>
                        <exclude name="previous.*" />
                 </methods>
                 </class>
            
        </classes>
    </test>
</suite>


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:

Output

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