简体   繁体   中英

Maven Cobertura: unit test failed but build success

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.

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 (eg 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.

You could set haltOnFailure property to true.

<configuration>
    ...
    <check>
        ...
        <haltOnFailure>true</haltOnFailure>
        ...
    </check>
</configuration>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM