[英]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-test和unitTests ,两者都绑定到测试目标,如下所示:
<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的设置如下:
mvn clean install -DskipTests
mvn clean install -DskipTests
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-cli和unitTests执行?
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.