Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
## Objective
Run [pytest](https://docs.pytest.org/en/latest/) 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.
### 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](https://docs.pytest.org/en/stable/customize.html) files.
Here's an example of a `pytest.ini` file:
```ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
```
This configuration file sets the minimum pytest version to 6.0, sets additional command line
options, and specifies the test directory.
And here's an example of pytest configuration in `pyproject.toml` (New in version 6.0.):
```python
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"tests",
]
```
## How to use it
1. Configure the `pytest.ini` or `pyproject.toml` file in your repository's root directory with your
desired rules.
2. Include the pytest template in your CI/CD configuration (see quick use above).
3. If you need to customize the job, check
the [jobs customization](https://docs.r2devops.io/get-started/use-templates/#job-templates-customization).
## Change the Python version
By default, the template uses the latest Python version available in the image. If you need to
change the Python version, you can override the docker image used in the template. For example:
```yaml
stages:
- test
include:
- remote: 'https://api.r2devops.io/job/r/gitlab/dorianturba/r2devops_catalog/pytest@latest.yaml'
pytest:
image: python:3.11
```
This example uses Python 3.11 instead of the latest Python version available.
## Add a <a href="https://docs.pytest.org/en/latest/"><img alt="Tested with pytest" src="https://img.shields.io/badge/Tested%20with-pytest-green"></a> Badge to your project README.md
To display the use of pytest in your project, you can add the following badge to your README.md:
```markdown
<a href="https://docs.pytest.org/en/latest/">
<img alt="Tested with pytest" src="https://img.shields.io/badge/Tested%20with-pytest-green">
</a>
```
Please note that the `src` URL in the pytest badge will need to be updated to reflect the actual
testing status of your project in your continuous integration pipeline.