-
Dorian Turba authoredDorian Turba authored
Objective
Run pytest on your Python code to ensure all unit tests pass. Pytest is a popular framework for writing and executing tests in Python, and it supports test automation, modular fixtures, parameterized testing, and many other features.
How to use it
- Configure the
pytest.ini
orpyproject.toml
file in your repository's root directory with your desired rules. - Include the pytest template in your CI/CD configuration (see quick use above).
- If you need to customize the job, check the jobs customization.
Variables
Name | Description | Default |
---|---|---|
PROJECT_PATH |
The path to the project root directory. | "." |
IMAGE_TAG |
The default tag for the docker image. See Python Docker Official Image for supported tags and respective Dockerfile links | "latest" |
PYTHON_SETUP |
Bash commands to setup your python environment. Default rely on pyproject.toml to install project and test dependencies. |
"pip install .[TESTS]" |
Global Configuration of pytest
To add configuration to pytest
that is shared with any other usage of pytest (such as manual runs,
pre-commit, etc.), you can use the pytest.ini
or pyproject.toml
configuration files. Learn more
about pytest configuration files.
Multiple pytest Jobs in the same pipeline
You can run this pytest job multiple times in the same pipeline (e.g. to test multiple python version or different namespaces) using extends GitLab keyword.
stages:
- test
include:
- remote: 'https://api.r2devops.io/job/r/gitlab/dorianturba/r2devops_catalog/pytest@latest.yaml'
# Compatibility tests
pytest_3.10:
extends: pytest
variables:
IMAGE_TAG: "3.10"
Badge to your project README.md
Add a To display the use of pytest in your project, you can add the following badge to your README.md:
<a href="https://docs.pytest.org/en/latest/">
<img alt="Tested with pytest" src="https://img.shields.io/badge/Tested%20with-pytest-green">
</a>