简体   繁体   English

没有组的TestNG mvn运行测试

[英]TestNG mvn run tests without group

I am using TestNG and Maven with surefire plugin to run tests. 我正在将TestNG和Maven与surefire插件一起使用以运行测试。 I have: 我有:

@Test(groups={"groupA"})
TestA{}

@Test
TestB

I would like to have possibility to run: 我希望可以运行:

mvn test 

should invoke all test without any group 应该在没有任何组的情况下调用所有测试

mvn test -Dgroups=groupA

should invoke only groupA tests ( this works by default but just added here to have it working with previous option ) 应该仅调用groupA测试(默认情况下有效,但仅在此处添加以使其与先前的选项一起使用)

I tried to configure surefire like: 我试图像这样配置surefire:

<plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.16</version>
 <configuration>
   <excludedGroups>groupA</excludedGroups>
 </configuration>
</plugin>

mvn test works as expected, but after mvn测试工作正常,但之后
mvn test -Dgroups=groupA no tests are executed mvn test -Dgroups = groupA不执行任何测试

Edit 编辑

I found solution here: https://labs.consol.de/blog/maven/citrus-and-testng-groups/ 我在这里找到了解决方案: 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>

But there is one problem with this solution. 但是这种解决方案存在一个问题。 It works fine when we want run just always one groups of tests, for example mvn test -P a-tests , but when we will add another group, let's say b-tests, then after mvn test -P a-tests, b-tests only one group will be executed because last defined profile will overide properties... Any ideas how to combine testGroupsExcluded , testGroups properties from multiple profiles? 当我们只想运行一组测试时,例如mvn test -P a-tests ,它运行良好,但是当我们要添加另一组测试时,假设b-tests,然后在mvn test -P a-tests之后运行b-测试仅执行一个组,因为最后定义的配置文件将覆盖属性...任何想法如何组合来自多个配置文件的testGroupsExcludedtestGroups属性?

Edit 2 编辑2

I just ended with solution 我刚刚解决了

 <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>

But I had to assign all my tests explicitly to groups (all unassigned tests now are 'unit'), but now I can call: 但是我必须将所有测试明确地分配给组(现在所有未分配的测试都是“单元”),但是现在我可以调用:

mvn test To invoke all tests marked as unit mvn test调用所有标记为单位的测试

mvn test -DtestGroups=groupA, groupB To invoke any group tests... mvn test -DtestGroups = groupA,groupB调用任何组测试...

Dude, have you checked http://testng.org/doc/documentation-main.html#beanshell ? 杜德(Dude),您是否检查过http://testng.org/doc/documentation-main.html#beanshell

In surefire plugin attach testng suit config: 在surefire插件中附加testng西服配置:

<configuration>
    <suiteXmlFiles>
        <suiteXmlFile>testng.xml</suiteXmlFile>
    </suiteXmlFiles>
...

in testng.xml 在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"));         
            ]]>

In static Java method shouldExecuteTest you can implement any rule you want! 在静态Java方法shouldExecuteTest中,您可以实现所需的任何规则!

Accoriding to doc, you can use those vars: 根据文档,您可以使用以下变量:

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") is just -Dgroups=xxx passed from mvn invocation. System.getProperty(“ groups”)只是从mvn调用传递的-Dgroups = xxx。

Works like a charm! 奇迹般有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM