Can maven surefire plugin run multiple test executions when invoked directly? (Sonar + Jenkins not running all unit tests)
Asked Answered
L

1

7

I have a maven project that uses the surefire plugin for unit testing. Currently the testing is split into two executions default-test and unitTests, both bound to the test goal, as follows:

<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
...
[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:

  • run the build action mvn clean install -DskipTests
  • run the Sonar plugin as a post-build action

This means that the Sonar plugin runs the unit tests, and the unit tests are run once only. And I see the following output when the Sonar plugin runs:

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

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. The unit tests configured in the unitTests execution are not run at all, even though that execution is bound to the same goal.

How can I ensure that the both the default-cli and unitTests executions are invoked when the Sonar plugin invokes mvn surefire:test ?

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

Liguria answered 31/3, 2014 at 0:25 Comment(0)
H
0

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.

That seems like a good idea. You could use maven profiles to do this. You can configure the surefire plugin in different profiles and make the one you want your tools to use active by default.

Honeysucker answered 14/8, 2018 at 16:20 Comment(1)
I had a similar issue recently, and this is the pom now: github.com/CROSSINGTUD/WPDS/blob/master/boomerangPDS/pom.xmlHoneysucker

© 2022 - 2024 — McMap. All rights reserved.