54

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)?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
ebnius
  • 910
  • 2
  • 8
  • 14
  • You should have provided more information. What programming language do you use? What build tool do you prefer? – luka5z Apr 28 '16 at 19:15
  • 1
    I have python, javascript and other sorts of projects and I have been using Cobertura on freestyle projects to display code coverage reports. However after switching to pipeline I don't get the post build UI to setup Cobertura for the project. – ebnius Apr 28 '16 at 19:25
  • 1
    Check if your plugin is [supported](https://github.com/jenkinsci/pipeline-plugin/blob/master/COMPATIBILITY.md) by Pipeline job. – luka5z Apr 28 '16 at 19:30
  • 6
    Looks like there's an issue open against the Cobertura plugin here: https://issues.jenkins-ci.org/browse/JENKINS-30700 – toast38coza May 31 '16 at 15:04

4 Answers4

48

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
Roman Kutlak
  • 2,684
  • 1
  • 19
  • 24
48

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

hwjp
  • 15,359
  • 7
  • 71
  • 70
  • 1
    Worked for me and is a lot easier than the original solution. Thanks! – Flame_Phoenix Dec 19 '18 at 10:01
  • Both of them work, but this one is a lot easier on the eyes in the Jenkinsfile. – Jay Prall Sep 20 '19 at 15:32
  • What version of Cobertura plugin is compatible for Jenkins 2.204.1 – Tiny Nov 26 '20 at 08:33
  • The full list of arguments can be found here: https://www.jenkins.io/doc/pipeline/steps/cobertura/ To list multiple arguments, simply place a comma after them (e.g. cobertura coberturaReportFile: 'path', sourceEncoding: 'ASCII'). The best way to see a description of what each property does is to use the Pipeline Syntax generator and select 'cobertura'. Unfortunately the documentation doesn't explain the format for the "coverage target" arguments, so those can be found here: https://github.com/jenkinsci/cobertura-plugin/issues/111 – Fearnbuster Jan 18 '22 at 16:42
7

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

chim
  • 8,407
  • 3
  • 52
  • 60
-2

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]
SACn
  • 1,862
  • 1
  • 14
  • 29