How to use regular expressions with TestNG?


We use regular expressions in TestNG to work with a group of test methods that are named with a certain pattern.

Example

Testng xml file.

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Tutorialspoint Test ">
   <test name = "Test Cycle">
      <classes>
         <class name = "TestRegularExpression" />
         <methods>
            <exclude name= “Payment.*”/>
         </methods>
      </classes>
   </test>
</suite>

All the test methods with starting name Payment will be excluded from the regression suite.

Example

@Test
public void PaymentHistory(){
   System.out.println("Payment history validation is successful”);
}
@Test
public void Login(){
   System.out.println("Login is successful”);
}
@Test
public void PaymentDefault(){
   System.out.println("Payment default verification is successful”);
}

Login() will be executed, but all the methods starting with name Payment will be excluded from execution. This is achieved using regular expression(Payment.*).

Updated on: 11-Jun-2020

878 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements