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