unit-testing - maven surefire 插件可以在直接调用时运行多个测试执行吗? (Sonar + Jenkins 没有运行所有单元测试)

标签 unit-testing maven jenkins sonarqube maven-surefire-plugin

我有一个使用surefire插件进行单元测试的maven项目。目前测试分为两个执行default-test和unitTests,都绑定(bind)到测试目标,如下:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
      <!-- Test one set of files -->
      <execution>
        <id>default-test</id>
        <goals>
          <goal>test</goal>
        </goals>
        <configuration>
          <includes> ... </includes>
        </configuration>
      </execution>
      <!-- Test a different set of files -->
      <execution>
        <id>unitTests</id>
        <goals>
          <goal>test</goal>
        </goals>
        <configuration>
          <includes> ... </includes>
        </configuration>
      <execution>
    </executions>
  </plugin>
</plugins>

这在运行 mvn test 时工作正常:
$ mvn test
...
[INFO] maven-surefire-plugin:2.13:test (default-test)
...
[INFO] maven-surefire-plugin:2.13:test (unitTests)

我在 Jenkins 上使用 Sonar 插件来生成代码指标,Jenkins 的设置如下:
  • 运行构建操作 mvn clean install -DskipTests
  • 将 Sonar 插件作为构建后操作运行

  • 这意味着 Sonar 插件运行单元测试,并且单元测试只运行一次。当 Sonar 插件运行时,我看到以下输出:
    [INFO] maven-surefire-plugin:213:test (default-cli) 
    

    请注意,只调用了 default-cli 执行而不是 default-test + unitTests,因为(我假设)surefire 插件是通过 mvn surefire:test 直接调用的。而不是通过mvn test生命周期。

    我可以在 POM 文件中添加一个 default-cli 执行,并从 default-test 执行复制配置,但这只会运行一组单元测试。 unitTests 中配置的单元测试执行根本不运行,即使该执行绑定(bind)到同一个目标。

    如何确保在 Sonar 插件调用 mvn surefire:test 时同时调用 default-cli 和 unitTests 执行?

    我在想也许我的 Jenkins 设置应该改变,以便单元测试在正常的构建操作中运行,生成代码覆盖率报告,然后 Sonar 插件作为构建后操作运行并加载这些报告以执行分析。不确定这是否可行,或者我的 POM 文件需要进行哪些更改才能支持这一点(希望对 POM 文件进行最小的更改是另一个目标)。

    最佳答案

    I am thinking that perhaps my Jenkins setup should change, so that the unit tests are run in the normal build action, which generates code coverage reports, and then the Sonar plugin runs as a post-build action and loads these reports to perform analysis.



    这似乎是个好主意。您可以使用 maven profiles去做这个。您可以在不同的配置文件中配置 surefire 插件,并使您希望您的工具默认使用的那个插件处于事件状态。

    关于unit-testing - maven surefire 插件可以在直接调用时运行多个测试执行吗? (Sonar + Jenkins 没有运行所有单元测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22751777/

    相关文章:

    java - Spring Boot 应用程序无法使用 Spring Boot Dashboard 在 Eclipse 中启动

    java - karaf-services-maven-plugin 上的 mvn 并行构建 -T

    java - *.war 文件无法在 Tomcat 上创建任何内容的主要原因是什么?

    javascript - Angular 单元测试 : Uncaught Error: Uncaught (in promise): Error: Cannot match any routes. URL 段: 'logout'

    javascript - 如何使用 javascript/jQuery 对箭头键导航进行单元测试

    c - 如何在 C 中模拟 Linux 系统调用

    java - Jenkins - 将源代码编译成 jarfile,用作工件

    maven - Jenkins 构建不尊重第三方存储库?

    git - 使用 Jenkins 和 Git 进行身份验证

    php - 使用 PHPUnit 处理路径问题