[英]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.