问题
Once upon a time, the pytest document now at https://docs.pytest.org/en/latest/goodpractices.html used to say:
avoid “
__init__.py
” files in your test directories. This way your tests can run easily against an installed version ofmypkg
, independently from the installed package if it contains the tests or not.
I don't understand following about that quote:
- What does it mean by installed version of
mypkg
? How can I relate installing an app if I have a simple flask app that only says "hello world" when you hit the root at the localhost? - How is installed version different from installed package?
- What does it mean "if it contains tests or not"? Is it related to test discovery?
- Can someone practically explain how not having
__init__.py
is good?
回答1:
py.test needs to make the tests importable by adding a directory to sys.path, if you have a test folder in the project root and a __init__.py
in it,
then py.test will pick the project root for path insertion and it thus always making the source tree importable first, which can hide bugs in the installed version while running tests
an alternative way to avoid the issue is having a src
folder and using the package_dirs option in setup.py
来源:https://stackoverflow.com/questions/29153922/pytest-and-why-avoid-init-file