Running PyTest in Jenkins helps automate testing your Python code every time you make changes. This ensures your code works well and catches errors early.
Running PyTest in Jenkins
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
pipeline {
agent any
stages {
stage('Run PyTest') {
steps {
sh 'pytest --junitxml=results.xml'
}
}
stage('Publish Test Results') {
steps {
junit 'results.xml'
}
}
}
}This is a Jenkins Pipeline script written in Groovy.
The sh step runs the PyTest command in the shell.
Examples
PyTest
sh 'pytest'PyTest
sh 'pytest --junitxml=results.xml'PyTest
junit 'results.xml'Sample Program
This Jenkins Pipeline runs PyTest to execute tests and then publishes the test results in Jenkins.
PyTest
pipeline {
agent any
stages {
stage('Run PyTest') {
steps {
sh 'pytest --junitxml=results.xml'
}
}
stage('Publish Test Results') {
steps {
junit 'results.xml'
}
}
}
}Important Notes
Make sure Python and PyTest are installed on the Jenkins agent machine.
The --junitxml option creates a file Jenkins can read to show test results.
Use the Jenkins junit step to display test results and mark build status.
Summary
Running PyTest in Jenkins automates your Python testing process.
Use a Jenkins Pipeline with sh 'pytest --junitxml=results.xml' to run tests.
Publish results with the junit step to see test outcomes in Jenkins.
Practice
1. What is the main purpose of running
pytest in Jenkins?easy
Solution
Step 1: Understand Jenkins role in testing
Jenkins automates tasks like running tests without manual effort.Step 2: Identify pytest's role
Pytest runs Python tests and generates results for Jenkins to display.Final Answer:
To automate running Python tests and see results in Jenkins -> Option DQuick Check:
Automation of tests = A [OK]
Hint: Jenkins runs tests automatically to show results [OK]
Common Mistakes:
- Confusing test running with deployment
- Thinking Jenkins writes tests
- Assuming Jenkins monitors server performance
2. Which Jenkins Pipeline command correctly runs pytest and saves results in XML format?
easy
Solution
Step 1: Recall pytest XML output option
The correct option to save results in XML is--junitxml=filename.Step 2: Match Jenkins shell command syntax
Jenkins usesshto run shell commands, sosh 'pytest --junitxml=results.xml'is correct.Final Answer:
sh 'pytest --junitxml=results.xml' -> Option AQuick Check:
Correct pytest XML flag = D [OK]
Hint: Use --junitxml=filename to save pytest results [OK]
Common Mistakes:
- Using wrong pytest flags for XML output
- Incorrect Jenkins shell command syntax
- Confusing XML output with other formats
3. Given this Jenkins Pipeline snippet:
What will Jenkins display after running this pipeline?
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'pytest --junitxml=results.xml'
junit 'results.xml'
}
}
}
}What will Jenkins display after running this pipeline?
medium
Solution
Step 1: Understand the
Theshstepshcommand runs pytest and generatesresults.xmlwith test results.Step 2: Understand the
Thejunitstepjunitstep readsresults.xmland shows test results in Jenkins UI.Final Answer:
Test results summary with passed and failed tests -> Option AQuick Check:
pytest + junit step = test summary shown [OK]
Hint: Use junit step to publish pytest XML results [OK]
Common Mistakes:
- Forgetting to add junit step to publish results
- Assuming pytest output alone shows results in Jenkins
- Confusing console logs with test reports
4. You run this Jenkins Pipeline:
But Jenkins shows an error:
sh 'pytest --junitxml=results.xml' junit 'results.xml'
But Jenkins shows an error:
FileNotFoundError: results.xml not found. What is the likely cause?medium
Solution
Step 1: Check if pytest ran successfully
If pytest fails or does not run, it won't createresults.xml.Step 2: Confirm file existence before junit step
Thejunitstep needs the XML file; if missing, it errors out.Final Answer:
Pytest did not run or failed before creating results.xml -> Option BQuick Check:
Missing XML means pytest didn't create it [OK]
Hint: Check pytest success before junit step [OK]
Common Mistakes:
- Assuming junit step spelling causes file error
- Ignoring pytest failure logs
- Not verifying file path correctness
5. You want Jenkins to run pytest only on files changed in a Git branch and publish results. Which approach best fits this requirement?
hard
Solution
Step 1: Detect changed files in Jenkins Pipeline
Use Git commands or plugins to find changed Python files in the branch.Step 2: Run pytest only on those changed files and publish results
Runpytestwith paths of changed files, then usejunitto show results.Final Answer:
Use Jenkins Pipeline to detect changed files, run pytest on those, then publish withjunit-> Option CQuick Check:
Selective test run + publish = A [OK]
Hint: Detect changed files, run pytest on them, publish results [OK]
Common Mistakes:
- Running all tests every time wasting resources
- Skipping automated test runs
- Not publishing test results in Jenkins
