gpt4 book ai didi

maven - 使用 Maven 拆分单元和集成测试

转载 作者:行者123 更新时间:2023-11-28 20:07:23 25 4
gpt4 key购买 nike

我正在 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”运行单元测试和集成测试。

我也尝试过使用这里描述的故障安全插件:Maven separate Unit Test and Integration Tests ,但无论我输入哪个选项,“mvn verify”都会运行单元测试和集成测试。

如何让我的管道在两个不同的步骤中执行单元测试和集成测试?

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>

具有 surefire 和 failsafe 的 Pom:

            <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>

最佳答案

这为我解决了:

下面是使用以下命令的示例 pom.xml:

  • mvn clean verify -DskipUTs=true:仅运行集成测试(以“IT”结尾的测试)
  • mvn clean verify -DskipITs=true:仅运行单元测试(以“Test”结尾的测试)
  • mvn clean verify -DskipTests=true : 跳过所有测试

    <build>
    <finalName>test-app</finalName>
    <plugins>

    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId> <!-- surefire plugin version managed by Spring Boot -->
    <configuration>
    <skipTests>${skipUTs}</skipTests>
    </configuration>
    </plugin>

    <plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M3</version>
    <executions>
    <execution>
    <id>run-integration-tests</id>
    <phase>integration-test</phase>
    <goals>
    <goal>integration-test</goal>
    <goal>verify</goal>
    </goals>
    </execution>
    </executions>
    <configuration>
    <skipTests>${skipTests}</skipTests>
    <skipITs>${skipITs}</skipITs>
    </configuration>
    </plugin>

    </plugins>
    </build>

关于maven - 使用 Maven 拆分单元和集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58413678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com