我有一个场景,我需要通过包含和排除某些testng组来运行测试。
考虑以下情况
import org.testng.annotations.Test;
public class GroupingTest {
@Test(groups = {"bat"})
public void batTest(){
System.out.println("Am bat");
}
@Test(groups = {"p1"})
public void p1Test(){
System.out.println("Am p1");
}
@Test(groups = {"p2"})
public void p2Test(){
System.out.println("Am p2");
}
@Test(groups = {"bat","p3"})
public void batp3Test(){
System.out.println("Am bat p3 ");
}
}
在这里,我如何只能运行“ bat”测试组,并且不应该运行“ 33”的“ bat”测试。 在上述情况下,当我运行..时,它应该只打印“ Am bat” 我该如何实现?有什么建议吗?
答案 0 :(得分:1)
基本上有两种方法可以完成此任务。
方法1:使用beanshell选择器
7.0.0-beta1
。<dependency>
<groupId>org.apache-extras.beanshell</groupId>
<artifactId>bsh</artifactId>
<version>2.0b6</version>
</dependency>
<?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:使用自定义方法选择器
7.0.0-SNAPSHOT
(我之所以这么说是因为,TestNG中存在一个错误,导致该功能无法正常工作。我继续进行了修复,并将其作为{{3 }}。但至今尚未发布)<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-SNAPSHOT</version>
</dependency>
要使用快照版本,可能需要在pom文件中添加如下所示的<repository>
标签。
<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) {}
}
<?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
===============================================