我正在Jenkins中设置CD流水线,并希望将我的单元测试和集成测试分别运行在两个步骤中。计划是让我的流水线脚本看起来像这样,并让它们分别运行:
stage('Unit tests') {
steps {
withMaven(maven: 'Maven 3.6.2') {
sh 'mvn test -P coverage'
}
}
}
stage('Integration tests') {
steps {
withMaven(maven: 'Maven 3.6.2') {
sh 'mvn test -P coverage'
}
}
我已经尝试按照这里描述的方法使用surefire插件:https://dzone.com/articles/splitting-unit-and-integration-tests-using-maven-a,运行'mvn test'只会运行单元测试,但是'mvn integration-test'会同时运行单元测试和集成测试。
我也尝试按照这里描述的方法使用failsafe插件:Maven separate Unit Test and Integration Tests,但是无论我输入哪些选项,'mvn verify'都会运行单元测试和集成测试。
如何使我的流水线在两个不同的步骤中执行单元测试和集成测试?
使用surefire的Pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <!-- surefire plugin version managed by Spring Boot -->
<configuration>
<skipTests>true</skipTests>
</configuration>
<executions>
<execution>
<id>unit-tests</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
<includes>
<include>**/*Test.java</include>
</includes>
</configuration>
</execution>
<execution>
<id>integration-tests</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skipTests>false</skipTests>
<includes>
<include>**/*IT.*</include>
<include>**/*Tests.*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
Pom与Surefire和Failsafe:
<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>
mvn clean verify -Prun-its
(仅在集成测试需要很长时间时使用配置文件)来完成...不要使用maven-surefire-plugin来运行集成测试...遵循惯例,使用单元测试名称*Test.java
,而集成测试名称为*IT.java
(详见文档)...不要按照你正在进行的方式配置surefire,因为已经存在非常好的默认值...在99.999%的情况下是正确的。 - khmarbaisemvn clean test
运行单元测试以及通过mvn clean verify
(可能需要补充-Prun-its
以运行集成测试)。 - khmarbaise