上QQ阅读APP看书,第一时间看更新
Tests that accompany your code
You can place your test modules together with the code they are testing by creating a tests folder next to the modules themselves:
setup.py
mylib/
tests/
__init__.py
test_core.py
test_utils.py
__init__.py
core.py
utils.py
By putting the tests near the code they test, you gain the following advantages:
- It is easier to add new tests and test modules in this hierarchy and keep them in sync
- Your tests are now part of your package, so they can be deployed and run in other environments
The main disadvantage with this approach is that some folks don't like the added package size of the extra modules, which are now packaged together with the rest of the code, but this is usually minimal and of little concern.
As an additional benefit, you can use the --pyargs option to specify tests using their module import path. For example:
λ pytest --pyargs mylib.tests
This will execute all test modules found under mylib.tests.
You might consider using _tests for the test module names instead of _test. This makes the directory easier to find because the leading underscore usually makes them appear at the top of the folder hierarchy. Of course, feel free to use tests or any other name that you prefer; pytest doesn't care as long as the test modules themselves are named test_*.py or *_test.py.