Cobertura code coverage report for jenkins pipeline jobs

JenkinsJenkins PipelineCobertura

Jenkins Problem Overview


I'm using the pipeline plugin for jenkins and I'd like to generate code coverage report for each run and display it along with the pipeline ui. Is there a plugin I can use to do that(e.g. Cobertura but it doesn't seem to be supported by pipeline)?

Jenkins Solutions


Solution 1 - Jenkins

There is a way to add a pipeline step to publish your coverage report but it doesn't show under the BlueOcean interface. It will show fine in the normal UI.

pipeline {
    agent any

    stages {
        ...
    }
    post {
        always {
            junit '**/nosetests.xml'
            step([$class: 'CoberturaPublisher', autoUpdateHealth: false, autoUpdateStability: false, coberturaReportFile: '**/coverage.xml', failUnhealthy: false, failUnstable: false, maxNumberOfBuilds: 0, onlyStable: false, sourceEncoding: 'ASCII', zoomCoverageChart: false])
        }
    }
}

Note that one of the parameters to the Cobertura plugin is the XML that it will use ('**/coverage.xml' in the example).

If you are using python, you will want to use something like:

nosetests --with-coverage --cover-xml --cover-package=pkg1,pkg2 --with-xunit test

Solution 2 - Jenkins

Nowadays you can also use the cobertura command directly in a Jenkinsfile

stage ("Extract test results") {
    cobertura coberturaReportFile: 'path-to/coverage.xml'
}

source: https://issues.jenkins-ci.org/browse/JENKINS-30700

Solution 3 - Jenkins

The answer from hwjp is correct, however there are extra parameters that you can add to the command that are not easy to find.

Once you have installed the Cobertura plugin, you can find the cobertura step options in

> Job Dashboard Page -> Pipeline Syntax -> Steps Reference

There's also a snippet generator which is really useful to get started at

> Job Dashboard Page -> Pipeline Syntax

example command:

cobertura coberturaReportFile: 'coverage.xml', enableNewApi: true, lineCoverageTargets: '80, 60, 70'

enableNewApi is a good one to set to true, as the new API is much prettier :D setting coverage targets will automatically fail the job if the code coverage is too low

Solution 4 - Jenkins

Generate report using command line cobertura-report in specified directory and attach results as artifacts.

cobertura-report   [--datafile   file]    --destination  dir  [--format       html|xml]  [--encoding encoding]  directory [--basedir dir]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionebniusView Question on Stackoverflow
Solution 1 - JenkinsRoman KutlakView Answer on Stackoverflow
Solution 2 - JenkinshwjpView Answer on Stackoverflow
Solution 3 - JenkinschimView Answer on Stackoverflow
Solution 4 - JenkinsSACnView Answer on Stackoverflow