How do I configure when cobertura tests run in maven-cobertura-plugin?
Asked Answered
D

1

9

In order to fine-tune which tests are run at which times and in which environments, we have several executions set up for the maven-surefire-plugin. We set the default configuration to skip all tests, then enable them for the executions we want. This by itself works well for us.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
     <skip>true</skip>
  </configuration>
  <executions>
     <execution>
       <id>unit-tests</id>
       <phase>test</phase>
       <goals>
          <goal>test</goal>
       </goals>
       <configuration>
          <skip>false</skip>
          <includes>
             <include>**/*Tests.java</include>
          </includes>
          <excludes>
             <exclude>**/*IntegrationTests.java</exclude>
          </excludes>
       </configuration>
     <execution>
     <execution>
       <id>integration-tests</id>
       <phase>integration-test</phase>
       <goals>
          <goal>test</goal>
       </goals>
       <configuration>
          <skip>false</skip>
          <includes>
             <include>**/*IntegrationTests.java</include>
          </includes>
       </configuration>
     <execution>
   </executions>
</plugin>

When I add the maven-cobertura-plugin to the mix, I run into problems. The cobertura goal runs, and successfully instruments my classes. However, no tests get run. I assume this is because the test execution that cobertura is running in is one that is skipped. However, I cannot find how to specify which phase and goal to set up for this execution. When I turn on all tests, the output seems to indicate that these are still running in these unit-tests and integration-tests phases/goals.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>2.4</version>
  <configuration>
     <formats>
        <format>xml</format>
        <format>html</format>
     </formats>
  </configuration>
  <executions>
     <execution>
        <phase>package</phase>
        <goals>
           <goal>cobertura</goal>
        </goals>
     </execution>
  </executions>
</plugin>

How do I need to specify a surefire execution so that the cobertura will run it against the instrumented classes?

Dyna answered 9/12, 2011 at 17:21 Comment(2)
When you run your tests without Cobertura, how are you telling maven to use different executions? Are you using Profiles?Zamora
The unit-tests is running during the regular test phase (so, when we do mvn package), while integration-tests runs when we do mvn install. I thought this was because phase and goal are specified in those executions, though I admit this might be misunderstanding on my part. We do use profiles to enable or disable Cobertura, but these do not seem related to choosing between these other tests.Dyna
N
2

You will note from the docs that cobertura:cobertura

  • Must be wired as a report
  • Instruments, tests and generates a report
  • Runs in its own lifecycle cobertura (not the default lifecycle)
  • Invokes lifecycle phase test before running itself

So, wiring it accordingly should automatically result in instrumentation and testing.

Ninety answered 10/12, 2011 at 1:25 Comment(7)
Yeah, I think I have done all that (probably should have included the report part). So if it is in lifecycle cobertura is there some way I can specify the lifecycle in my executions? As I said, the instrumentation does appear to be happening, it just tells me that the tests are skipped.Dyna
If you wire it as suggested (mojo.codehaus.org/cobertura-maven-plugin/usage.html) it will automatically instrument, run tests and then generate the report.Ninety
The basic configuration worked with a stripped down project, but still fails in my real project build. So it seems that something else in the pom is interfering.Dyna
That you are able to get it to work in some configuration is a good start. From that state I suggest adding just your source code (plus of course any dependency modifications to the POM). See how that goes. Then tweak the POM further.Ninety
Perhaps what he is asking, is what I'm trying to do as well. I can generate the cobertura reports by building with "mvn cobertura:cobertura" but my build is run with "mvn clean install". This does run cobertura tests to check for coverage, but doesn't build the report. I'd like (and perhaps the OP too) to have these reports generated every time I build with "mvn clean install" or "mvn clean package". Is that possible?Selfaddressed
@KevinM I haven't worked in this space in a couple of years now. Unable to add more to this discussion. Sorry.Ninety
@CPhelps, I believe you need to specify .class files instead of .javaSeel

© 2022 - 2024 — McMap. All rights reserved.