Skip to content

Add docs on coverage and testing #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/_toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ parts:
- caption: Python packaging
chapters:
- file: "part1/packaging"
- file: "part1/testing"
- file: "part1/coverage"
- file: "part1/linting"
- file: "part1/typing"
- file: "part1/testing"
- file: "part1/pre_commit.md"

- caption: Github tools
Expand Down
25 changes: 25 additions & 0 deletions docs/part1/coverage.md
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
# Coverage reports

When creating a Python package, it can be very useful to know what part of your code is covered by the tests.

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.

We add to the `addopts` section of the `[tool.pytest.ini_options]` table:
```toml
addopts = [
# Other options...
"--cov=mypackage --cov-report html --cov-report term-missing -v"
]

We use Github Actions to upload the coverage report as an artifact after executing the tests. We add the following step
```yml
- name: Run tests
run: python -m pytest

- name: Upload coverage report as artifact
if: matrix.os == 'ubuntu-22.04' && matrix.python-version == '3.10'
uses: actions/upload-artifact@v3
with:
name: code-coverage-report
path: htmlcov
if-no-files-found: error
```
34 changes: 34 additions & 0 deletions docs/part1/testing.md
Original file line number Diff line number Diff line change
@@ -1 +1,35 @@
# Testing

It is very important to have tests for verification of your code.
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.

The most important types of tests are the _unit tests_, which tests the core functionality of your code.

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
```bash
python3 -m pip install pytest
```

You can run `pytest` as
```bash
python3 -m pytest
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.

We add the following information to `pyproject.toml` under table header: `[project.optional-dependencies]`
```toml
[project.optional-dependencies]
# Other entries
# ....
test = [
"pytest",
]

[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
# Other entries ....
]
```
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.

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)
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ files = [ # Folder to which files that should be checked by mypy
]

[tool.pytest.ini_options]
addopts = "--cov=mypackage --cov-report html --cov-report term-missing -v"
addopts = [
"--import-mode=importlib",
"--cov=mypackage",
"--cov-report=html",
"--cov-report=term-missing",
"-v"]

testpaths = [
"test"
]