4

I've configured cobertura code coverage in my pom:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
    <instrumentation>
        <excludes>
            <exclude>**/*Exception.class</exclude>
        </excludes>
    </instrumentation>
    <formats>
        <format>xml</format>
        <format>html</format>
    </formats>
</configuration>
</plugin>

And start test by following command:

mvn clean cobertura:cobertura

But if one of unit test fail Cobertura only log this information and doesn't mark build fail.

Tests run: 287, Failures: 1, Errors: 1, Skipped: 0

Flushing results...
Flushing results done
Cobertura: Loaded information on 139 classes.
Cobertura: Saved information on 139 classes.
[ERROR] There are test failures.

.................................

[INFO] BUILD SUCCESS

How to configure Cobertura marks build failed in one of unit test fail?

Thanks in advance.

Pavel Drobushevich
  • 308
  • 1
  • 4
  • 10
  • Are you certain you aren't trying to do too much in one sweep? Usual builds stop on unit test failure, but coverage builds kinda serve different purposes. I don't know your exact case, but why do you want Cobertura to fail your tests instead of run them all and tell you what coverage you have? – LAFK 4Monica_banAI_modStrike Aug 15 '13 at 09:15

2 Answers2

3

If you run a special goal from the cobertura plugin you can not force maven to fail the build if a test was not passed successfully. The plugin goal will succeed.

You can bind the cobertura run to a lifecycle phase (e.g. test). This will make the cobertura goal run with this phase (mvn clean test) and fail if this phase fails.

        <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>test</phase>
                    <goals>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

The disadvantage of this solution is that the cobertura goal will run ich each test phase.

FrVaBe
  • 47,963
  • 16
  • 124
  • 157
  • Thank you for answer. This solution works, but in this case maven run unit tests twice. If I use option _-Dmaven.test.skip=true_, maven doesn't run any tests. Could you please clarify how to fix it? I've found following cobertura bug [MCOBERTURA-83](http://jira.codehaus.org/browse/MCOBERTURA-83). Are there any other workarounds to fix tests double start without source patching? – Pavel Drobushevich Mar 01 '11 at 08:01
  • I do not know if this can be fixed. In this case there is one "normal" test run (surefire plugin) and one "intrumented test run" (cobertura). I doubt that this can be "merged" into one run. – FrVaBe Mar 01 '11 at 08:37
0

You could set haltOnFailure property to true.

<configuration>
    ...
    <check>
        ...
        <haltOnFailure>true</haltOnFailure>
        ...
    </check>
</configuration>
Raghuram
  • 51,854
  • 11
  • 110
  • 122