Skip to content

Commit 2900c66

Browse files
authored
Add docs on coverage and testing (#39)
* Add docs on coverage and testing * Fix coverage * Apply suggestions from code review
1 parent 1c3bb9b commit 2900c66

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

docs/_toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ parts:
55
- caption: Python packaging
66
chapters:
77
- file: "part1/packaging"
8+
- file: "part1/testing"
89
- file: "part1/coverage"
910
- file: "part1/linting"
1011
- file: "part1/typing"
11-
- file: "part1/testing"
1212
- file: "part1/pre_commit.md"
1313

1414
- caption: Github tools

docs/part1/coverage.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
11
# Coverage reports
2+
3+
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
15+
```yml
16+
- name: Run tests
17+
run: python -m pytest
18+
19+
- name: Upload coverage report as artifact
20+
if: matrix.os == 'ubuntu-22.04' && matrix.python-version == '3.10'
21+
uses: actions/upload-artifact@v3
22+
with:
23+
name: code-coverage-report
24+
path: htmlcov
25+
if-no-files-found: error
26+
```

docs/part1/testing.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,35 @@
11
# Testing
2+
3+
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 for test 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)

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ files = [ # Folder to which files that should be checked by mypy
5555
]
5656

5757
[tool.pytest.ini_options]
58-
addopts = "--cov=mypackage --cov-report html --cov-report term-missing -v"
58+
addopts = [
59+
"--import-mode=importlib",
60+
"--cov=mypackage",
61+
"--cov-report=html",
62+
"--cov-report=term-missing",
63+
"-v"]
64+
5965
testpaths = [
6066
"test"
6167
]

0 commit comments

Comments
 (0)