2014-03-06 93 views
0

我使用TestNG和Maven with surefire插件来运行测试。 我:TestNG mvn运行测试无组

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

@Test 
TestB 

我想有可能运行:

mvn test 

应该调用所有测试没有任何组

mvn test -Dgroups=groupA 

应该只调用A组试验(这是通过默认,但只是在这里添加使它与以前的选项)

我想配置万无一失,如:如预期

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

MVN测试作品,但经过
MVN测试-Dgroups = A组 没有测试执行

编辑

我在这里找到解决方案: 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测试-P a-tests,但是当我们将添加另一个组时,我们说b-测试,然后在之后mvn测试-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测试要调用标记为单元的所有测试

MVN测试-DtestGroups = A组,B组调用任何组测试...

回答

0

老兄,你检查http://testng.org/doc/documentation-main.html#beanshell? 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中,你可以实现你想要的任何规则!

Accoriding到文档中,则可以使用这些增值经销商:

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( “组”)刚刚从-Dgroups调用MVN传递= XXX。

工程就像一个魅力!