0
0
Jenkinsdevops~30 mins

Running unit tests in pipeline in Jenkins - Mini Project: Build & Apply

Choose your learning style9 modes available
Running unit tests in pipeline
📖 Scenario: You are working on a software project that uses Jenkins to automate building and testing code. To ensure your code is reliable, you want to run unit tests automatically every time you update the code. This project will guide you to create a simple Jenkins pipeline script that runs unit tests.
🎯 Goal: Build a Jenkins pipeline script that defines a basic pipeline with a stage to run unit tests using a shell command.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Add an agent any directive to run on any available agent
Define a stage named Run Unit Tests
Inside the stage, run the shell command ./run_tests.sh to execute unit tests
Print the output of the test command in the pipeline logs
💡 Why This Matters
🌍 Real World
Automating unit tests in Jenkins pipelines helps catch bugs early and keeps code quality high.
💼 Career
Knowing how to write Jenkins pipelines to run tests is a key skill for DevOps engineers and automation specialists.
Progress0 / 4 steps
1
Create the basic Jenkins pipeline structure
Write a Jenkins pipeline script starting with pipeline { and add agent any inside it. This sets up the pipeline to run on any available Jenkins agent.
Jenkins
Need a hint?

Start your Jenkinsfile with pipeline { and add agent any inside the block.

2
Add a stage to run unit tests
Inside the pipeline block, add a stages block. Then add a stage named Run Unit Tests inside stages. This stage will hold the commands to run your tests.
Jenkins
Need a hint?

Use stages {} to group stages, then add stage('Run Unit Tests') {} inside it.

3
Add a step to run the test script
Inside the stage('Run Unit Tests') block, add a steps block. Inside steps, add a shell command to run ./run_tests.sh using sh. This will execute your unit tests.
Jenkins
Need a hint?

Use steps { sh './run_tests.sh' } to run the test script.

4
Print the test results in the pipeline output
Add a post block inside the stage('Run Unit Tests') block. Inside post, add an always block that runs a shell command to print Test stage completed using sh. This confirms the test stage finished.
Jenkins
Need a hint?

Use post { always { sh 'echo Test stage completed' } } inside the stage to print a message after tests run.