An open API service indexing awesome lists of open source software.

https://github.com/aldaviva/cobertura-maven-plugin

Another Maven plugin to measure Java code coverage using Cobertura, this time with build-failing thresholds and tests that only run once.
https://github.com/aldaviva/cobertura-maven-plugin

cobertura coverage maven-plugin

Last synced: about 2 months ago
JSON representation

Another Maven plugin to measure Java code coverage using Cobertura, this time with build-failing thresholds and tests that only run once.

Awesome Lists containing this project

README

        

cobertura-maven-plugin
===

This is a [Maven](https://maven.apache.org/) plugin that lets you measure code coverage of your tests using
[Cobertura](https://cobertura.github.io/cobertura/). It's a different project than the official
[`org.codehaus.mojo:cobertura-maven-plugin`](http://www.mojohaus.org/cobertura-maven-plugin/).

## Features

- Tests run once, not twice
- Minimum thresholds for line or branch coverage percentage that will allow the build to succeed
- Exclude certain files from coverage threshold checks
- Cobertura is compatible with testing utilities that modify bytecode, like the fantastic
[PowerMock](https://powermock.github.io/), because classes are instrumented on disk before tests start, not during
runtime with a JVM agent like the otherwise-fantastic [JaCoCo](http://www.eclemma.org/jacoco/).

## Usage

### Installation

Add these snippets to your POM.

```xml




net.sourceforge.cobertura
cobertura
2.1.1
test






com.aldaviva.coverage
cobertura-maven-plugin
0.0.1-SNAPSHOT



Ant path syntax:
** = multiple directories
* = single directory or multiple characters
? = single character
-->
**/*Exception.class
com/mycompany/myprogram/Untestable.class



0.80
0.80




instrument

instrument

process-test-classes



check

check

prepare-package




```

### Execution example
```text
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Sample project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sample-project ---
[INFO] Deleting C:\Users\Ben\Documents\Projects\sample-project\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sample-project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ sample-project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\Users\Ben\Documents\Projects\sample-project\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sample-project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Ben\Documents\Projects\sample-project\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ sample-project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\Ben\Documents\Projects\sample-project\target\test-classes
[INFO]
[INFO] --- cobertura-maven-plugin:0.0.1-SNAPSHOT:instrument (instrument) @ sample-project ---
[INFO] Cobertura: Saved information on 1 classes.
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sample-project ---
[INFO] Surefire report directory: C:\Users\Ben\Documents\Projects\sample-project\target\surefire-reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.aldaviva.coverage.MainTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@1963006a
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.402 sec
[INFO] Cobertura: Loaded information on 1 classes.
[INFO] Cobertura: Saved information on 1 classes.

Results :

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

[INFO]
[INFO] --- cobertura-maven-plugin:0.0.1-SNAPSHOT:check (check) @ sample-project ---
[INFO] Cobertura: Loaded information on 1 classes.
[INFO] Line coverage: 85.7% actual, 100.0% required.
[INFO] Branch coverage: 50.0% actual, 100.0% required.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.410 s
[INFO] Finished at: 2017-04-09T07:28:17-07:00
[INFO] Final Memory: 19M/248M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.aldaviva.coverage:cobertura-maven-plugin:0.0.1-SNAPSHOT:check (check) on project sample-project: Coverage checks not met -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[INFO] Cobertura: Loaded information on 1 classes.
[INFO] Cobertura: Saved information on 1 classes.
```

Cobertura's data file will be saved as `cobertura.ser` in the `${project.baseDir}` directory, so your build machine can generate useful coverage reports.

## Acknowledgements
- [jacoco-maven-plugin](http://www.eclemma.org/jacoco/trunk/doc/maven.html) for showing me the light that build systems shouldn't have to run tests twice just to measure coverage.
- [Michel Pawlak](http://www.pawlak.ch/)'s [qualinsight-mojo-cobertura](https://github.com/QualInsight/qualinsight-mojo-cobertura) plugin for the idea to backup, instrument, and restore the entire `classes` directory.