Maven 独立的单元测试和集成测试
Maven separate Unit Test and Integration Tests
UT = 单元测试
IT = 集成测试。我所有的集成测试 类 都带有 @Category(IntegrationTest.class)
注释
我的目标是:
mvn clean install
=> 运行 UT 而不是 IT
mvn clean install
-DskipTests=true => 没有执行任何测试
mvn clean deploy
=> 运行 UT 而不是 IT
mvn clean test
=> 运行 UT 而不是 IT
mvn clean verify
=> 运行 UT 和 IT
mvn clean integration-test
=> 运行 IT 和 UT 未执行
mvn clean install deploy
=> 运行 UT 而不是 IT
pom 属性:
<junit.version>4.12</junit.version>
<surefire-plugin.version>2.18.1</surefire-plugin.version>
<failsafe-plugin.version>2.18.1</failsafe-plugin.version>
编译器:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
单元测试:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<excludedGroups>com.xpto.IntegrationTest</excludedGroups>
</configuration>
</plugin>
集成测试:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe-plugin.version}</version>
<configuration>
<groups>com.xpto.IntegrationTest</groups>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
我的结果是:
mvn clean install
=> 好的
mvn clean install
-DskipTests=true => OK
mvn clean deploy
=> 运行 UT 而不是 IT
mvn clean test
=> 好的
mvn clean verify
=> NOK ...只执行了UT但还需要执行IT
mvn clean integration-test
=> NOK ... UT 已执行且不应执行,IT 未执行但应执行
mvn clean install deploy
=> 好的
解决方法是这样的:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>${surefire.skip}</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
这将让您控制执行哪个。
运行 UT 和 IT:
mvn clean verify
运行 是 IT 但不是 UT
mvn clean verify -Dsurefire.skip=true
运行 UT 但不是 IT:
mvn clean verify -DskipITs=true
您需要遵循插件的命名约定,这会让生活更轻松。
Naming conventions 用于 maven-surefire-plugin(单元测试)。
Naming conventions 用于 maven-failsafe-plugin(集成测试)。
UT = 单元测试 IT = 集成测试。我所有的集成测试 类 都带有 @Category(IntegrationTest.class)
注释我的目标是:
mvn clean install
=> 运行 UT 而不是 IT
mvn clean install
-DskipTests=true => 没有执行任何测试
mvn clean deploy
=> 运行 UT 而不是 IT
mvn clean test
=> 运行 UT 而不是 IT
mvn clean verify
=> 运行 UT 和 IT
mvn clean integration-test
=> 运行 IT 和 UT 未执行
mvn clean install deploy
=> 运行 UT 而不是 IT
pom 属性:
<junit.version>4.12</junit.version>
<surefire-plugin.version>2.18.1</surefire-plugin.version>
<failsafe-plugin.version>2.18.1</failsafe-plugin.version>
编译器:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> <compilerArgument>-Xlint:all</compilerArgument> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin>
单元测试:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>${surefire-plugin.version}</version> <configuration> <excludedGroups>com.xpto.IntegrationTest</excludedGroups> </configuration> </plugin>
集成测试:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>${failsafe-plugin.version}</version> <configuration> <groups>com.xpto.IntegrationTest</groups> </configuration> <executions> <execution> <goals> <goal>integration-test</goal> </goals> <configuration> <includes> <include>**/*.class</include> </includes> </configuration> </execution> </executions> </plugin>
我的结果是:
mvn clean install
=> 好的
mvn clean install
-DskipTests=true => OK
mvn clean deploy
=> 运行 UT 而不是 IT
mvn clean test
=> 好的
mvn clean verify
=> NOK ...只执行了UT但还需要执行IT
mvn clean integration-test
=> NOK ... UT 已执行且不应执行,IT 未执行但应执行
mvn clean install deploy
=> 好的
解决方法是这样的:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>${surefire.skip}</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
这将让您控制执行哪个。
运行 UT 和 IT:
mvn clean verify
运行 是 IT 但不是 UT
mvn clean verify -Dsurefire.skip=true
运行 UT 但不是 IT:
mvn clean verify -DskipITs=true
您需要遵循插件的命名约定,这会让生活更轻松。
Naming conventions 用于 maven-surefire-plugin(单元测试)。 Naming conventions 用于 maven-failsafe-plugin(集成测试)。