-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docs on coverage and testing (#39)
* Add docs on coverage and testing * Fix coverage * Apply suggestions from code review
- Loading branch information
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters