[英]TestNG mvn run tests without group
我正在将TestNG和Maven与surefire插件一起使用以运行测试。 我有:
@Test(groups={"groupA"})
TestA{}
@Test
TestB
我希望可以运行:
mvn test
应该在没有任何组的情况下调用所有测试
mvn test -Dgroups=groupA
应该仅调用groupA测试(默认情况下有效,但仅在此处添加以使其与先前的选项一起使用)
我试图像这样配置surefire:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<excludedGroups>groupA</excludedGroups>
</configuration>
</plugin>
mvn测试工作正常,但之后
mvn test -Dgroups = groupA不执行任何测试
编辑
我在这里找到了解决方案: https : //labs.consol.de/blog/maven/citrus-and-testng-groups/
<!-- TestNG groups -->
<testGroups></testGroups>
<testGroupsExcluded>groupA</testGroupsExcluded>
<!-- /TestNG groups-->
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<groups>${testGroups}</groups>
<excludedGroups>${testGroupsExcluded}</excludedGroups>
</configuration>
...
<profile>
<id>a-testes</id>
<properties>
<testGroups>a</testGroups>
<testGroupsExcluded></testGroupsExcluded>
</properties>
</profile>
但是这种解决方案存在一个问题。 当我们只想运行一组测试时,例如mvn test -P a-tests ,它运行良好,但是当我们要添加另一组测试时,假设b-tests,然后在mvn test -P a-tests之后运行b-测试仅执行一个组,因为最后定义的配置文件将覆盖属性...任何想法如何组合来自多个配置文件的testGroupsExcluded和testGroups属性?
编辑2
我刚刚解决了
<properties>
<testGroups>unit</testGroups>
</properties>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<groups>${testGroups}</groups>
</configuration>
</plugin>
但是我必须将所有测试明确地分配给组(现在所有未分配的测试都是“单元”),但是现在我可以调用:
mvn test调用所有标记为单位的测试
mvn test -DtestGroups = groupA,groupB调用任何组测试...
杜德(Dude),您是否检查过http://testng.org/doc/documentation-main.html#beanshell ?
在surefire插件中附加testng西服配置:
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
...
在testng.xml中
<suite name="Emap test suite">
<test name="Emap tests">
<method-selectors>
<method-selector>
<script language="beanshell"><![CDATA[
addClassPath("target/test-classes" );
return com.yourpackage.shouldExecuteTest(groups, System.getProperty("groups"));
]]>
在静态Java方法shouldExecuteTest中,您可以实现所需的任何规则!
根据文档,您可以使用以下变量:
java.lang.reflect.Method method: the current test method.
org.testng.ITestNGMethod testngMethod: the description of the current test method
java.util.Map<String, String> groups: a map of the groups the current test method belongs to.
System.getProperty(“ groups”)只是从mvn调用传递的-Dgroups = xxx。
奇迹般有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.