pytest and why avoid init file

隐身守侯 提交于 2019-12-12 16:14:16

问题


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 of mypkg, independently from the installed package if it contains the tests or not.

I don't understand following about that quote:

  1. 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?
  2. How is installed version different from installed package?
  3. What does it mean "if it contains tests or not"? Is it related to test discovery?
  4. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!