TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

How to use Regex in TestNG | Running Test Cases with Regex

How to use Regex in TestNG | Running Test Cases with Regex with Testng Tutorial, Introduction, Testng Installation, Features of Testng, Suite Test, Testng Exception Test, Group Test, Plug with Ant, Plug with Eclipse, etc.

<< Back to HOW

Running test cases with Regex

In this topic, we will use the regular expressions to include/exclude test methods from the test suite execution.

Now we will consider an example to understand how to use Regex for running test cases in TestNG.

Step 1: Let's create a java project.

package com.TheDeveloperBlog;

import org.testng.annotations.Test;

public class test 
{
  @Test
  public void WebLoginCarLoan()
  {
	  System.out.println("WebLoginCarLoan");
  }
  @Test
  public void MobileLoginCarLoan()
  {
	  System.out.println("MobileLoginCarLoan");
  }
  @Test
  public void MobileLoginPersonalLoan()
  {
	  System.out.println("MobileLoginPersonalLoan");
  }
  @Test
  public void MobileLoginHomeLoan()
  {
	  System.out.println("MobileLoginHomeLoan");
  }
  @Test
  public void LoginAPICarLoan()
  {
	  System.out.println("LoginAPICarLoan");
  }
}

Step 2: Till now, we have created the java file. If we want to include only those test cases which are starting with a keyword "Mobile". To achieve this, we need to configure testing.xml file and after configuration, it would look like:












 
 

Note: The pattern /sequence .*/ searches the string which is starting with the sequence keyword including space character. The '*' asterisk represents the remaining characters.

In the above testing.xml configuration file, we include all the test cases represented by the starting keyword 'Mobile' with a pattern Mobile.* in <include> tag.

Step 3: Run the testng.xml file. Right click on the testng.xml file and move the cursor down, click on the 1 TestNG Suite.

Running test cases with Regex

Output

Running test cases with Regex

In the above case, we use regular expression in a <include> tag. We can also use the regular expression in <exclude> tag as well.

Let's understand through an example.

Step 1: Let's create a simple java project.

package com.TheDeveloperBlog;

import org.testng.annotations.Test;

public class exclude 
{
@Test
	public void employeeid()
 {
	 System.out.println("EmployeeID");
 }
 @Test
 public void employee_name()
 {
	 System.out.println("Employee Name");
 }
 @Test
 public void employee_address()
 {
	 System.out.println("Employee Address");
 }
 @Test
 public void owner_name()
 {
	 System.out.println("Owner Name");
 }
}

Step 2: Now we want to exclude those test methods which are starting with a keyword "employee", we use a regular expression in a <exclude> tag. To achieve this, we need to configure the testng.xml file and its configuration would look like:




 






 
 

Step 3: Run the testng.xml file.

Output

Running test cases with Regex
Next TopicTestNG Groups




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf