Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest and why avoid init file

Tags:

python

pytest

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?
like image 540
LuckyStarr Avatar asked Oct 16 '25 13:10

LuckyStarr


1 Answers

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!