How to Use pytest in CI/CD Pipelines for Automated Testing
To use
pytest in CI/CD, add a test step in your pipeline configuration that runs pytest commands to execute tests automatically on each code change. This ensures tests run in a clean environment and results are reported back to your CI/CD system for pass/fail feedback.Syntax
The basic command to run pytest in CI/CD is pytest [options]. Common options include:
--junitxml=path: outputs test results in XML format for CI tools.-v: verbose output showing each test name.--maxfail=num: stops after a number of failures.
In CI/CD, you typically run pytest in a shell script or pipeline step to execute tests and generate reports.
bash
pytest -v --junitxml=results.xml
Example
This example shows a simple GitHub Actions workflow that runs pytest on every push. It installs dependencies, runs tests, and uploads test results.
yaml
name: Python Tests on: [push] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.10' - name: Install dependencies run: | python -m pip install --upgrade pip pip install pytest - name: Run tests run: pytest -v --junitxml=results.xml - name: Upload test results uses: actions/upload-artifact@v3 with: name: pytest-results path: results.xml
Output
Test session starts
collected 2 items
test_sample.py::test_add PASSED
test_sample.py::test_subtract PASSED
================= 2 passed in 0.05s =================
Common Pitfalls
Common mistakes when using pytest in CI/CD include:
- Not installing dependencies before running tests, causing failures.
- Not generating test reports, so CI tools can't show results.
- Running tests without a clean environment, leading to flaky tests.
- Ignoring exit codes, which causes CI to pass even if tests fail.
Always ensure your pipeline installs all needed packages, runs pytest with proper options, and respects the exit code.
bash
## Wrong way (no dependencies installed) run: pytest ## Right way run: | pip install -r requirements.txt pytest -v --junitxml=results.xml
Quick Reference
| Command/Step | Purpose |
|---|---|
| pip install pytest | Install pytest in CI environment |
| pytest -v | Run tests with verbose output |
| pytest --junitxml=results.xml | Generate XML report for CI |
| Check pytest exit code | Fail CI if tests fail |
| Upload test reports | Make results available in CI UI |
Key Takeaways
Add a pytest test step in your CI/CD pipeline to automate test runs on code changes.
Always install dependencies before running pytest to avoid test failures.
Use the --junitxml option to generate test reports readable by CI tools.
Ensure your pipeline respects pytest's exit codes to catch test failures.
Upload or publish test reports so your team can see test results easily.