简体   繁体   English

maven surefire插件可以在直接调用时运行多个测试执行吗? (声纳+詹金斯没有运行所有单元测试)

[英]Can maven surefire plugin run multiple test executions when invoked directly? (Sonar + Jenkins not running all unit tests)

I have a maven project that uses the surefire plugin for unit testing. 我有一个使用surefire插件进行单元测试的maven项目。 Currently the testing is split into two executions default-test and unitTests , both bound to the test goal, as follows: 目前测试分为两个执行default-testunitTests ,两者都绑定到测试目标,如下所示:

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

This works fine when running mvn test : 这在运行mvn test时工作正常:

$ mvn test
...
[INFO] maven-surefire-plugin:2.13:test (default-test)
...
[INFO] maven-surefire-plugin:2.13:test (unitTests)

I am using the Sonar plugin on Jenkins to generate code metrics, and Jenkins is set up as follows: 我在Jenkins上使用Sonar插件生成代码指标,Jenkins的设置如下:

  • run the build action mvn clean install -DskipTests 运行构建操作mvn clean install -DskipTests
  • run the Sonar plugin as a post-build action 运行Sonar插件作为后构建操作

This means that the Sonar plugin runs the unit tests, and the unit tests are run once only. 这意味着Sonar插件运行单元测试,单元测试仅运行一次。 And I see the following output when the Sonar plugin runs: 当Sonar插件运行时,我看到以下输出:

[INFO] maven-surefire-plugin:213:test (default-cli) 

Note that only the default-cli execution is invoked rather than default-test + unitTests , because (I assume) the surefire plugin is invoked directly via mvn surefire:test instead of via the mvn test lifecycle. 请注意,只调用default-cli执行而不是default-test + unitTests ,因为(我假设)通过mvn surefire:test而不是通过mvn test生命周期直接调用surefire插件。

I can add a default-cli execution to the POM file, and copy the configuration from the default-test execution, but this will only run one set of unit tests. 我可以向POM文件添加default-cli执行,并从默认测试执行中复制配置,但这只会运行一组单元测试。 The unit tests configured in the unitTests execution are not run at all, even though that execution is bound to the same goal. unitTests执行中配置的单元测试根本不运行,即使该执行绑定到同一目标。

How can I ensure that the both the default-cli and unitTests executions are invoked when the Sonar plugin invokes mvn surefire:test ? 当Sonar插件调用mvn surefire:test时,如何确保调用default-cliunitTests执行?

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. 我想也许我的Jenkins设置应该改变,以便单元测试在正常构建操作中运行,生成代码覆盖率报告,然后Sonar插件作为构建后操作运行并加载这些报告以执行分析。 Not sure if this is possible, or what changes are required to my POM files to support this (hoping to make minimal changes to POM files is another goal). 不确定这是否可行,或者我的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. 我想也许我的Jenkins设置应该改变,以便单元测试在正常构建操作中运行,生成代码覆盖率报告,然后Sonar插件作为构建后操作运行并加载这些报告以执行分析。

That seems like a good idea. 这似乎是一个好主意。 You could use maven profiles to do this. 您可以使用maven配置文件来执行此操作。 You can configure the surefire plugin in different profiles and make the one you want your tools to use active by default. 您可以在不同的配置文件中配置surefire插件,并默认使您希望工具使用的插件处于活动状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 maven surefire插件是否需要运行单元测试? - Is the maven surefire plugin needed to run unit tests? Maven surefire插件-按顺序运行所有测试 - maven surefire plugin - run all tests sequentially Maven surefire插件是否使用多个线程运行测试? - Does Maven surefire plugin run tests using multiple threads? 如何在运行“maven clean install”和Sonar时只运行一次单元测试? - How to only run unit tests once when running “maven clean install” and Sonar? Maven Surefire:运行单个单元测试 - Maven Surefire: run a single unit test Maven-surefire-plugin测试在Jenkins构建中失败,但在本地成功运行? - Maven-surefire-plugin tests fail in Jenkins build but run successfully locally? maven surefire测试插件即使被排除在外也会运行测试: - maven surefire test plugin runs tests even if they are excluded: 错误消息:尝试在不建立连接的情况下运行命令当使用单元测试运行多个测试时 - Error Message: Tried to run command without establishing a connection When running multiple tests with unit test "在一个单元测试项目中运行多个单元测试" - Running multiple Unit Tests in an unit test project 在解决方案中运行所有测试时的单元测试顺序 - Unit test sequence when running all tests in solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM