📖 Scenario: You are working on a software project that uses Jenkins for continuous integration. Your team wants to track how much of the code is tested by automated tests. This helps find untested parts and improve software quality.
🎯 Goal: Set up a Jenkins pipeline that runs tests and generates a code coverage report using a simple configuration. You will create the pipeline script step-by-step to collect and display coverage results.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add a stage called Run Tests that runs a shell command to execute tests and generate coverage data
Add a stage called Publish Coverage that uses the publishCoverage step to publish the coverage report
Print a message confirming the coverage report was published
💡 Why This Matters
🌍 Real World
Code coverage reports help teams see how much of their code is tested automatically. This improves software quality and reduces bugs.
💼 Career
Many DevOps and CI/CD roles require setting up pipelines that run tests and publish coverage reports to ensure code quality.
Progress0 / 4 steps
1
Create the Jenkins pipeline skeleton
Create a Jenkins pipeline script starting with pipeline { and agent any inside it. Add an empty stages block inside the pipeline.
Jenkins
Hint
Start with pipeline {, then add agent any and an empty stages block.
2
Add a stage to run tests and generate coverage
Inside the stages block, add a stage named Run Tests. Inside it, add a steps block that runs the shell command sh 'make test coverage' to execute tests and generate coverage data.
Jenkins
Hint
Use stage('Run Tests') with steps { sh 'make test coverage' } inside.
3
Add a stage to publish the coverage report
Add another stage inside stages named Publish Coverage. Inside it, add a steps block that calls publishCoverage adapters: [coberturaAdapter('coverage.xml')] to publish the coverage report from the file coverage.xml.
Jenkins
Hint
Use publishCoverage adapters: [coberturaAdapter('coverage.xml')] inside the Publish Coverage stage.
4
Print confirmation message after publishing coverage
Inside the Publish Coverage stage's steps block, after publishing coverage, add a echo step that prints 'Coverage report published successfully.'.
Jenkins
Hint
Add echo 'Coverage report published successfully.' after the publishCoverage step.
Practice
(1/5)
1. What is the main purpose of code coverage reports in Jenkins?
easy
A. To show which parts of the code are tested
B. To deploy the application automatically
C. To monitor server CPU usage
D. To manage user permissions
Solution
Step 1: Understand code coverage reports
Code coverage reports indicate which parts of the source code have been executed during tests.
Step 2: Identify Jenkins role
Jenkins uses these reports to help developers see test completeness and improve code quality.
Final Answer:
To show which parts of the code are tested -> Option A
Quick Check:
Code coverage = tested code parts [OK]
Hint: Coverage reports show tested code areas, not deployment [OK]
Common Mistakes:
Confusing coverage with deployment
Thinking coverage monitors server stats
Mixing coverage with user management
2. Which Jenkins plugin is commonly used to publish Java code coverage reports?
easy
A. Git Plugin
B. JaCoCo Plugin
C. Docker Plugin
D. Slack Notification Plugin
Solution
Step 1: Identify coverage plugins for Java
JaCoCo is a popular plugin for Java projects to generate code coverage reports in Jenkins.
Step 2: Exclude unrelated plugins
Git manages source code, Docker manages containers, Slack sends messages, so they don't handle coverage.
Final Answer:
JaCoCo Plugin -> Option B
Quick Check:
Java coverage = JaCoCo [OK]
Hint: JaCoCo is the standard for Java coverage in Jenkins [OK]
Common Mistakes:
Choosing Git for coverage
Confusing Docker with coverage tools
Selecting notification plugins
3. Given this Jenkins pipeline snippet, what will be the result of the jacoco step?