UT =单元测试 IT =集成测试。我的所有Integration测试类都使用@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
=&gt;行
mvn clean install
-DskipTests = true =&gt;行
mvn clean deploy
=&gt;运行UT而不是IT
mvn clean test
=&gt;行
mvn clean verify
=&gt; NOK ...只执行UT但是IT也需要执行
mvn clean integration-test
=&gt; NOK ... UT已执行且不应执行且IT未执行但应执行
mvn clean install deploy
=&gt;行
答案 0 :(得分:24)
解决方案是:
<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(集成测试)。