基本上有两种方法可以完成这项工作。
方法 #1:使用 beanshell 选择器
- 确保您使用的是最新发布的 TestNG 版本。从今天起是
7.0.0-beta1。
- 添加对 beanshell 的依赖(以下是使用 maven 时的操作方式)
<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<version>2.0b6</version>
</dependency>
- 将您的 TestNG 套件 xml 更改为如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53799427_suite" parallel="false" verbose="2">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[
whatGroup = System.getProperty("group");
shouldRun = Arrays.equals(new String[]{whatGroup}, testngMethod.getGroups());
return shouldRun;
]]>
</script>
</method-selector>
</method-selectors>
<test name="53799427_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn53799427.TestClassSample"/>
</classes>
</test>
</suite>
这里的测试类com.rationaleemotions.stackoverflow.qn53799427.TestClassSample 看起来与您分享的示例完全相同。
现在,当您通过传递 JVM 参数 -Dgroup=bat 来运行此套件 xml 文件时,您将看到如下所示的输出(之后的内容)
...
... TestNG 7.0.0-beta1 by Cédric Beust (cedric@beust.com)
...
Am bat
PASSED: batTest
===============================================
53799427_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
53799427_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
方法 #2:使用自定义方法选择器
- 确保您依赖于 TestNG
7.0.0-SNAPSHOT(我之所以这么说是因为 TestNG 中存在一个错误,导致此功能无法正常工作。我继续将其作为 GITHUB-1985 的一部分进行修复。但是它今天尚未发布)
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
要使用快照版本,您可能需要将如下所示的<repository> 标签添加到您的 pom 文件中。
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
- 现在创建一个自定义的
org.testng.IMethodSelector 实现,如下所示:
import java.util.Arrays;
import java.util.List;
import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;
public class FilteringMethodSelector implements IMethodSelector {
@Override
public boolean includeMethod(
IMethodSelectorContext context, ITestNGMethod method, boolean isTestMethod) {
String whichGroup = System.getProperty("group", "all");
if ("all".equalsIgnoreCase(whichGroup)) {
return true;
}
boolean isEqual = Arrays.equals(new String[]{whichGroup}, method.getGroups());
if (context != null) {
context.setStopped(true);
}
return isEqual;
}
@Override
public void setTestMethods(List<ITestNGMethod> testMethods) {}
}
- 创建如下所示的 testng 套件 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="53799427_suite" parallel="false" verbose="2">
<method-selectors>
<method-selector>
<selector-class
name="com.rationaleemotions.stackoverflow.qn53799427.FilteringMethodSelector" priority="0"/>
</method-selector>
</method-selectors>
<test name="53799427_test">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn53799427.TestClassSample"/>
</classes>
</test>
</suite>
这里的测试类com.rationaleemotions.stackoverflow.qn53799427.TestClassSample 看起来与您共享的示例完全相同。
现在,当您通过传递 JVM 参数 -Dgroup=bat 运行此套件 xml 文件时,您将看到如下所示的输出(之后的内容)
...
... TestNG 7.0.0-SNAPSHOT by Cédric Beust (cedric@beust.com)
...
Am bat
PASSED: batTest
===============================================
53799427_test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
53799427_suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================