Skip to content
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

update Python chapter testing section #260

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions best_practices/language_guides/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,19 @@ For packaging your code, you can either use `pip` or `conda`. Neither of them is

## Testing

* [pytest](http://pytest.org/latest/) is a full featured Python
testing tool. You can use it with `unittest`.
[Pytest intro](http://pythontesting.net/framework/pytest/pytest-introduction/)
* [Using mocks in Python](https://docs.python.org/3/library/unittest.mock.html)
* [unittest](https://docs.python.org/3/library/unittest.html) is a
framework available in Python Standard Library.
[Dr.Dobb's on Unit Testing with Python](http://www.drdobbs.com/testing/unit-testing-with-python/240165163)
* [doctest](https://docs.python.org/3/library/doctest.html) searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown. Always use this if you have example code in your documentation to make sure your examples actually work.

Using `pytest` is preferred over `unittest`, `pytest` has a much more concise syntax and supports many useful features.

Please make sure the command `python setup.py test` can be used to run your tests. When using `pytest`, this can be easily configured as described in the [`pytest` documentation](https://docs.pytest.org/en/latest/goodpractices.html#integrating-with-setuptools-python-setup-py-test-pytest-runner).
Use [pytest](https://docs.pytest.org/) as the basis for your testing setup.
This is preferred over the `unittest` standard library, because it has a much more concise syntax and supports many useful features.

It [has many plugins](https://docs.pytest.org/en/stable/plugins.html).
For linting, we have found `pytest-pycodestyle`, `pytest-pydocstyle`, `pytest-mypy` and `pytest-flake8` to be useful.
Other plugins we had good experience with are `pytest-cov`, `pytest-html`, `pytest-xdist` and `pytest-nbmake`.

Creating mocks can also be done within the pytest framework by using the `mocker` fixture provided by the `pytest-mock` plugin or by using `MagicMock` and `patch` from `unittest`.
For a general explanation about mocking, see the [standard library docs on mocking](https://docs.python.org/3/library/unittest.mock.html).

To run your test suite, it can be convenient to use `tox`.
Testing with `tox` allows for keeping the testing environment separate from your development environment.
The development environment will typically accumulate (old) packages during development that interfere with testing; this problem is avoided by testing with `tox`.

### Code coverage

Expand Down