0
0
Jenkinsdevops~5 mins

Code coverage reports in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Code coverage reports show how much of your code is tested by automated tests. They help find untested parts so you can improve your tests and catch bugs early.
When you want to check if your automated tests cover most of your code.
When you want to prevent untested code from being added to your project.
When you want to track test coverage over time to improve software quality.
When you want to fail a build if coverage drops below a certain level.
When you want to generate visual reports to share with your team.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the project...'
        // Add build commands here
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests...'
        // Run tests with coverage enabled, e.g., using Maven or Gradle
        sh 'mvn clean test'
      }
      post {
        always {
          junit '**/target/surefire-reports/*.xml'
          cobertura coberturaReportFile: '**/target/site/cobertura/coverage.xml'
        }
      }
    }
  }
}

This Jenkinsfile defines a pipeline with two stages: Build and Test.

In the Test stage, it runs tests with coverage enabled (example uses Maven).

After tests, it publishes test results with junit and coverage reports with cobertura plugin.

The coberturaReportFile points to the coverage XML file generated by the test tool.

Commands
Runs the tests and generates the code coverage report using Maven with the coverage plugin enabled.
Terminal
mvn clean test
Expected OutputExpected
[INFO] Scanning for projects... [INFO] [INFO] ------------------< com.example:my-app >------------------- [INFO] Building my-app 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-app --- [INFO] Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.345 s - in com.example.AppTest [INFO] [INFO] --- cobertura-maven-plugin:2.7:check (default) @ my-app --- [INFO] Cobertura coverage: 85% [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.123 s [INFO] Finished at: 2024-06-01T12:00:00Z [INFO] ------------------------------------------------------------------------
Triggers the Jenkins job named 'my-app' to start the pipeline that includes running tests and generating coverage reports.
Terminal
jenkins-jobs build my-app
Expected OutputExpected
Started build #42 for job my-app Building in workspace /var/lib/jenkins/workspace/my-app [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Building the project... [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo Running tests... [Pipeline] sh + mvn clean test [INFO] Scanning for projects... ... [INFO] BUILD SUCCESS [Pipeline] junit Recording test results [Pipeline] cobertura Publishing Cobertura coverage report [Pipeline] } [Pipeline] // stage [Pipeline] End of Pipeline Finished: SUCCESS
Fetches the HTML report page for the code coverage of build number 42 from Jenkins to view coverage details in the browser.
Terminal
curl http://jenkins.example.com/job/my-app/42/cobertura/
Expected OutputExpected
<html> <head><title>Code Coverage Report</title></head> <body> <h1>Coverage Report for Build #42</h1> <p>Line Coverage: 85%</p> <p>Branch Coverage: 80%</p> <!-- More detailed coverage info here --> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: code coverage reports show which parts of your code are tested, helping you improve test quality and software reliability.

Common Mistakes
Not generating the coverage report file before publishing it in Jenkins.
Jenkins cannot find the coverage XML file, so the report step fails or shows no data.
Ensure your test tool runs with coverage enabled and generates the coverage XML file before Jenkins tries to publish it.
Using incorrect file paths for the coverage report in the Jenkinsfile.
Jenkins cannot locate the coverage report file, so the coverage plugin shows errors or empty reports.
Use the exact relative path to the coverage XML file generated by your test tool in the Jenkins workspace.
Not installing or enabling the Jenkins coverage plugin (e.g., Cobertura plugin).
Jenkins does not recognize the coverage report step and fails the pipeline.
Install and enable the appropriate coverage plugin in Jenkins before using coverage report steps.
Summary
Run tests with coverage enabled to generate coverage report files.
Use Jenkins pipeline steps to publish test results and coverage reports.
View coverage reports in Jenkins UI or fetch them via HTTP for review.