2014-03-06 94 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。

工程就像一個魅力!