You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating a Python package, it can be very useful to know what part of your code is covered by the tests.
4
+
5
+
We recommend using [pytest-cov](https://pytest-cov.readthedocs.io/en/latest/) which extends the [pytest](./testing.md) suite with a coverage report of your package.
6
+
7
+
We add to the `addopts` section of the `[tool.pytest.ini_options]` table:
8
+
```toml
9
+
addopts = [
10
+
# Other options...
11
+
"--cov=mypackage --cov-report html --cov-report term-missing -v"
12
+
]
13
+
14
+
We use Github Actions to upload the coverage report as an artifact after executing the tests. We add the following step
It is very important to have tests for verification of your code.
4
+
There are several types of tests, see for instanche [Atlassian](https://www.atlassian.com/continuous-delivery/software-testing/types-of-software-testing) for a summary.
5
+
6
+
The most important types of tests are the _unit tests_, which tests the core functionality of your code.
7
+
8
+
The most common testing suite for Python in [pytest](https://docs.pytest.org/en/latest/), which can be installed from the [Python Package Index](https://docs.pytest.org/en/latest/) (PYPI) using
9
+
```bash
10
+
python3 -m pip install pytest
11
+
```
12
+
13
+
You can run `pytest` as
14
+
```bash
15
+
python3 -m pytest
16
+
Pytest will then find all files with names like `test_*.py` and `*_test.py`, see: [Conventions fortest discovery](https://docs.pytest.org/en/latest/explanation/goodpractices.html#test-discovery) for more information.
17
+
18
+
We add the following information to `pyproject.toml` under table header: `[project.optional-dependencies]`
19
+
```toml
20
+
[project.optional-dependencies]
21
+
# Other entries
22
+
# ....
23
+
test = [
24
+
"pytest",
25
+
]
26
+
27
+
[tool.pytest.ini_options]
28
+
addopts = [
29
+
"--import-mode=importlib",
30
+
# Other entries ....
31
+
]
32
+
```
33
+
The last option is due to pytest's recommendation for new projects, see [Choosing an import mode](https://docs.pytest.org/en/latest/explanation/goodpractices.html#choosing-an-import-mode) for more information.
34
+
35
+
For other inputs to `[tool.pytest.ini_options]` see: [https://docs.pytest.org/en/latest/reference/customize.html#pyproject-toml](https://docs.pytest.org/en/latest/reference/customize.html#pyproject-toml)
0 commit comments