C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Running test cases with RegexIn 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.
Output
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
Next TopicTestNG Groups
|