- Always add type hints including the return value.
- The type of the
mockerfixture isMockerFixture. It is imported asfrom pytest_mock import MockerFixture.
- The type of the
- All test function names must start with
test_. - Test files must be named
test_*.py. - Use
@pytest.mark.parametrizefor parametrised tests. - Use
@pytest.mark.asynciofor asynchronous tests. - Use
pytest.raisesfor testing exceptions. - Use
@pytest.fixturefor fixtures. - Do not create new fixtures inside test files.
- All fixtures must be defined in the
tests/conftest.pyfile. - Do not add docstrings to test functions or methods.
- Mock external dependencies and IO operations in tests.
- Prefer exercising the package under test through its public APIs. Do not import names beginning
with
_from that package, and do notmocker.patchthose names, except when patching a third-party native binding that upstream exposes under a leading underscore (as many C extensions do). - If a parameter must exist in a callback, use
_as the identifier if it is not a keyword argument. - Use the
runnerfixture (typeCliRunner) fromconftest.pyfor testing Click commands. - Test Click commands by checking
result.exit_codeandresult.output. - Use
mocker.patchfor complex mock setups;monkeypatch.setattrfor simple attribute replacement.