[英]How to make integration tests and unit tests run separately through maven?
[英]How to run unit tests in excludedGroups in Maven
我有一個JUnit 4.12 SlowTests
測試套件,除非在Maven命令行上有特殊要求,否則我希望將其從執行中排除。
我已將以下內容添加到我的pom文件中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<excludedGroups>com.Example.SlowTests</excludedGroups>
<includes>
<include>**/*TestSuite.class</include>
</includes>
<excludes>
<exclude></exclude>
</excludes>
</configuration>
</plugin>
我已將類別定義為SlowTests
並將其應用於MySlowTests
類。
我對測試套件的注釋如下:
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses({ MySlowTests.class })
public class MySlowTestSuite
當我運行mvn package
,將執行除MySlowTests
之外的所有單元測試。
但是,在查看各種答案(例如https://stackoverflow.com/a/25639372/820657和https://stackoverflow.com/a/21830866/820657)時,我期望使用以下命令:
mvn package -Dgroups=com.Example.MySlowTests
運行排除的MySlowTests
測試,但它們不會運行。 實際上沒有任何測試。
我究竟做錯了什么?
Maven Surefire插件在版本<2.13(IIRC)中存在一些類別問題,但是只要您使用的是Surefire> = 2.13,則以下代碼將運行帶有@Category(com.yourcompany.SlowTests.class)
注釋的任何類:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>com.yourcompany.SlowTests</groups>
</configuration>
</plugin>
這種方法通常用於配置文件,以下配置...
<profiles>
<profile>
<id>slow</id>
<properties>
<testCategories>com.yourcompany.SlowTests</testCategories>
</properties>
</profile>
<profile>
<id>fast</id>
<properties>
<testCategories>com.yourcompany.FastTests</testCategories>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>${testCategories}</groups>
</configuration>
</plugin>
</plugins>
</build>
...可用於運行:
mvn install -P slow
:僅運行慢速測試 mvn install -P fast
:僅運行快速測試 mvn install -P fast,slow
:運行快速和慢速測試 更新1:針對以下問題: “是否有一種方法可以使用這種方法,以便默認情況下可以運行所有快速測試?”
您可以定義兩個屬性:
<properties>
<includedCategories></includedCategories>
<excludedCategories>com.yourcompany.SlowTests</excludedCategories>
</properties>
然后像這樣更新您的surefire插件定義:
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<groups>${includedCategories}</groups>
<excludedGroups>${excludedCategories}</excludedGroups>
</configuration>
</plugin>
最后,添加此配置文件:
<profile>
<id>slow</id>
<properties>
<includedCategories>com.yourcompany.SlowTests</includedCategories>
<excludedCategories></excludedCategories>
</properties>
</profile>
這只是切換了includedCategories
和excludedCategories
屬性。 默認情況下,您包括除以com.yourcompany.SlowTests
注釋的那些測試以外的所有內容(即除“慢”測試以外的所有內容)。 當使用-P slow
運行時,您將排除所有用com.yourcompany.SlowTests
注釋的測試(即,除“ slow”測試以外的所有內容)。
注意:我在原始答案中對Surefire版本<2.13與類別分類不正確的說法仍然存在,因此要進行此工作,您需要使用Maven Surefire插件版本> = 2.13。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.