0
0
Jenkinsdevops~10 mins

Code coverage reports in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Code coverage reports
Start Jenkins Job
Run Tests with Coverage
Generate Coverage Report
Publish Report in Jenkins
View Coverage Results
Decide on Code Quality
End
The Jenkins job runs tests with coverage enabled, generates a report, publishes it, and then you view the results to check code quality.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Test') {
      steps {
        sh 'pytest --cov=./ --cov-report=xml'
      }
    }
    stage('Publish Coverage') {
      steps {
        publishCoverage adapters: [coberturaAdapter('coverage.xml')]
      }
    }
  }
}
This Jenkins pipeline runs tests with coverage and publishes the coverage report.
Process Table
StepActionCommand/ToolResult
1Start Jenkins jobJenkinsJob triggered and environment prepared
2Run tests with coveragepytest --cov=./ --cov-report=xmlTests run, coverage data collected
3Generate coverage reportpytest outputs coverage.xmlcoverage.xml file created
4Publish coverage reportpublishCoverage with coberturaAdapterCoverage report published in Jenkins UI
5View coverage resultsUser views Jenkins coverage tabCoverage percentages displayed
6Decide on code qualityUser reviews reportDecide to accept or improve code coverage
💡 All steps complete, coverage report available for review
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
coverage_datanonecollected during testssaved in coverage.xmlpublished in Jenkinsavailable for viewing
Key Moments - 2 Insights
Why do we need to run tests with coverage enabled before generating the report?
Because coverage data is collected during test execution (see execution_table step 2). Without running tests, there is no data to generate a report.
What does the publishCoverage step do in Jenkins?
It takes the coverage report file (coverage.xml) and makes it visible in Jenkins UI (see execution_table step 4). This helps users see coverage results easily.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the coverage.xml file created?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Check the 'Generate coverage report' action in the execution_table
According to the variable tracker, when is coverage_data first collected?
AAfter Step 3
BAfter Step 1
CAfter Step 2
DAfter Step 4
💡 Hint
Look at coverage_data changes in variable_tracker after each step
If the publishCoverage step is skipped, what happens to the coverage report visibility?
AReport is still visible in Jenkins UI
BReport is generated but not visible in Jenkins UI
CTests won't run
DCoverage data won't be collected
💡 Hint
Refer to execution_table step 4 about publishing coverage
Concept Snapshot
Jenkins Code Coverage Reports:
- Run tests with coverage enabled (e.g., pytest --cov)
- Coverage data saved in a report file (coverage.xml)
- Use publishCoverage step to show report in Jenkins UI
- View coverage results to assess code quality
- Helps ensure tests cover important code parts
Full Transcript
In Jenkins, code coverage reports are created by running tests with coverage enabled, which collects data about which parts of the code were tested. This data is saved in a file like coverage.xml. Then, Jenkins uses a publishCoverage step to display this report in its user interface. Users can view the coverage percentages and decide if the code quality is good or needs improvement. The process starts with triggering the Jenkins job, running tests, generating the report, publishing it, and finally viewing the results.