TestNG 正则表达式
使用正则表达式运行测试用例
在本主题中,我们将使用正则表达式在测试套件执行中包含/排除测试方法。
现在我们将考虑一个示例来了解如何使用 Regex 在 TestNG 中运行测试用例。
第 1 步: 让我们创建一个 java 项目。
package com.lidihuo;
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");
}
}
第 2 步 : 至此,我们已经创建了 java 文件。如果我们只想包含以关键字"Mobile"开头的那些测试用例。为此,我们需要配置 testing.xml 文件,配置后,它看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test_suite">
<test name="test">
<classes>
<class name="com.lidihuo.test">
<methods>
<include name="Mobile.*"/>
</methods>
</class>
</classes>
</test> <!--Test-->
</suite> <!--Suite-->
注意: 模式/sequence .*/搜索以序列关键字开头的字符串,包括空格字符。 '*' 星号代表剩余的字符。
在上面的testing.xml配置文件中,我们包含了所有由起始关键字"Mobile"表示的测试用例,其模式为Mobile。* 在 <include> 标记中。
第 3 步: 运行 testng.xml 文件。右键单击 testng.xml 文件并向下移动光标,单击 1 TestNG Suite。
输出
以上情况,我们在 <include> 标签中使用正则表达式。我们也可以在<exclude>标签中使用正则表达式。
我们通过一个例子来理解。
第 1 步: 让我们创建一个简单的 Java 项目。
package com.lidihuo;
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");
}
}
第 2 步: 现在我们要排除那些以关键字"employee"开头的测试方法,我们在
标签中使用正则表达式。为此,我们需要配置 testng.xml 文件,其配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="test_suite">
<test name="test"> <classes>
<class name="com.lidihuo.exclude">
<methods>
<exclude name="employee.*"/>
</methods>
</class>
</classes>
</test> <!--Test-->
</suite> <!--Suite-->
第 3 步: 运行 testng.xml 文件。
输出
