How to use Regex in TestNG?
Last Updated :
01 Apr, 2024
In this article, we will see how to use Regex in TestNG. In TestNG, we can use Regular Expressions for two purposes:
- To Include Test Cases
- 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
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
<?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.geeksforgeeks.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:
OutputSome 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
<?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.geeksforgeeks.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:
OutputExample 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
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite">
<test name="test1">
<groups>
<run>
<include name="high.*"/>
</run>
</groups>
<classes>
<class name="com.geeksforgeeks.test.RegexGroup"/>
</classes>
</test>
</suite>
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:
OutputNow in the testng.xml file, you can specify a regular expression pattern to exclude specific groups:
XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite">
<test name="test1">
<groups>
<run>
<exclude name="high.*"/>
</run>
</groups>
<classes>
<class name="com.geeksforgeeks.test.RegexGroup"/>
</classes>
</test>
</suite>
Output:
Output
Similar Reads
TestNG Tutorial
TestNGÂ is an automation testing framework widely used across many projects. NG means âNext Generationâ and it is influenced by JUnit and it follows the annotations (@).Table of Content What is TestNG?PrerequisiteAdvantages of TestNGTestNG Basic To AdvanceConclusionFAQs on TestNG TutorialWhat is Test
3 min read
How to Install TestNG on Eclispse IDE?
Because of its design and inspired functionality, TestNG is often seen as a more versatile and powerful testing framework than JUnit and NUnit. Enabling TPM (Trusted Platform Module) and Secure Boot in the BIOS is essential for ensuring that your Windows 11 installation meets the security requiremen
5 min read
Running test cases in TestNG without java compiler
How to Test Java Application using TestNG?
TestNG is an automation testing framework widely getting used across many projects. NG means âNext Generationâ and it is influenced by JUnit and it follows the annotations (@). Â End-to-end testing is easily handled by TestNG. As a sample, let us see the testing as well as the necessity to do it via
4 min read
How to Execute Failed Test Cases in TestNG
TestNG is a testing framework that simplifies many testing needs. It stands for Test Next Generation, an open-source test automation framework inspired by JUnit and NUnit. Think of TestNG as an updated version of the other two concepts. It provides additional features not previously available, such
7 min read
Unit Testing, Integration Testing, Priority Testing using TestNG in Java
TestNG is an automated testing framework. In this tutorial, let us explore more about how it can be used in a software lifecycle. Unit Testing Instead of testing the whole program, testing the code at the class level, method level, etc., is called Unit Testing The code has to be split into separate
6 min read
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: To Include Test CasesTo 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 i
5 min read
TestNG Annotations in Selenium Webdriver with Examples
TestNG is a testing framework widely used in Selenium WebDriver for automation testing. It provides a wide range of annotations that help in organizing and controlling the flow of test cases. TestNG learns from JUnit and NUnit, making itself better by adding new features that make testing easier and
6 min read
TestNG Annotations - @BeforeSuite
The Concept Annotations is introduced in Java 1.5. The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn th
2 min read
TestNG Annotations - @AfterSuite
The Concept Annotations is introduced in Java 1.5. The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGroupsIn th
2 min read
TestNG Annotations - @BeforeTest
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGrou
2 min read
TestNG @AfterTest Annotation
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGrou
3 min read
TestNG @BeforeClass Annotations
The Concept Annotations is introduced in Java 1.5 (jdk5). The Popular Annotation in Java is @override. We use the same annotation concept in TestNG. In TestNG, there are 10 Annotations @BeforeSuite@AfterSuite@BeforeTest@AfterTest@BeforeClass@AfterClass@BeforeMethod@AfterMethod@BeforeGroups@AfterGrou
3 min read