【问题标题】:Build parent pom and run specific integration test in Jenkins through maven?通过maven在Jenkins中构建父pom并运行特定的集成测试?
【发布时间】:2016-03-31 19:16:04
【问题描述】:

在我的父 pom 上执行 mvn clean install 会构建所有子模块并运行相关的 junit 测试。它不运行集成测试。

构建并运行junit后,我想在特定的子模块中运行特定的集成测试。

我正在使用以下命令:

mvn clean install -DTest=integrationTestName

作业在构建阶段失败并出现No Test Found 错误。

我也试过

mvn clean install -DTest=integrationTestName -pl=sub-module-name

但这只会构建我的具有集成测试的子模块。

问题:如何运行某个模块的单次集成测试?

【问题讨论】:

    标签: maven jenkins continuous-integration integration-testing


    【解决方案1】:

    我想您尝试了 Maven Surefire 插件的 test 选项(小写)来调用特定测试,Surefire 在反应堆构建的第一个模块中找不到该测试,因此失败了。

    我还假设集成测试由Maven Failsafe Plugin 执行。如果没有,他们应该按照官方文档中的说明:

    Failsafe 插件旨在运行集成测试,而 Surefire 插件旨在运行单元测试。 ...如果您使用 Surefire 插件运行测试,那么当您遇到测试失败时,构建将在integration-test 阶段停止,并且您的集成测试环境不会被正确拆除。 .. Failsafe 插件在integration-test 阶段不会使构建失败,从而使post-integration-test 阶段能够执行。

    简而言之:这样做更安全、更健壮。

    虽然Maven Failsafe Plugin的插件配置入口也是test,但其对应的命令行选项是it.test,所以你应该改为运行:

    mvn clean install -Dit.test=SampleIT
    

    其中SampleIT 是一个集成测试(注意标准的IT 后缀,recognized by default by Failsafe。

    Running a Single Test 官方文档还提供了有关运行单个集成测试的更多详细信息。


    另请注意:如果您在构建的其他模块中没有其他集成测试,上述调用将起作用,否则它将无法更早地找到它(在点击相关模块之前)。

    如果您使用 Surefire 运行相关集成测试(同样,您不应该)或者您有多个模块运行集成测试,那么您应该在相关模块中配置一个配置文件来处理此特定调用,如下所示:

    <profiles>
        <profile>
            <id>run-single-it-test</id>
            <activation>
                <property>
                    <name>single.it.test</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <configuration>
                            <test>${single.it.test}</test>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    然后调用构建如下:

    mvn clean install -Dsingle.it.test=SampleIT
    

    因此,Maven 将 activate the profile 基于 single.it.test 属性的值的存在并将其传递给故障安全插件(或 Surefire,如果是这种情况)的 test 属性,故障安全将忽略该执行的任何其他集成测试,并且其他模块不会受到任何影响(忽略该属性)。

    【讨论】:

      【解决方案2】:

      A_Di-Matteo 的回答可以帮助您完成大部分工作,但您需要以下 maven-surefire-plugin 配置来抑制所有单元测试。

      <profiles>
          <profile>
              <id>run-single-it-test</id>
              <activation>
                  <property>
                      <name>single.it.test</name>
                  </property>
              </activation>
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-surefire-plugin</artifactId>
                          <configuration>
                              <skipTests>true</skipTests>
                              <failIfNoTests>false</failIfNoTests>
                          </configuration>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>test</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-failsafe-plugin</artifactId>
                          <configuration>
                              <test>${single.it.test}</test>
                              <failIfNoTests>false</failIfNoTests>
                          </configuration>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>integration-test</goal>
                                      <goal>verify</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                  </plugins>
              </build>
          </profile>
      </profiles>
      

      【讨论】:

        猜你喜欢
        • 2012-02-22
        • 1970-01-01
        • 2016-03-09
        • 2017-09-30
        • 2018-08-29
        • 1970-01-01
        • 2010-09-30
        • 2016-06-05
        相关资源
        最近更新 更多