This should be the easiest problem on earth, but even after extensive searching and tinkering, I'm still in deep trouble with finding a "correct" way to lay a directory structure and manage to run pytest etc correctly.
Let's say my I have a program called apple.
|- README.md
|- apple
| |-- __init__.py
| |-- apple.py
| - tests
| |-- test_everything.py
The apple.py contains some functions, for examples sake let's call one eat()
. And the test_everything.py
file contains some tests like assert eat()=="foobar"
. So good so easy, but then the fun begins:
- What about the
__init__.py
in the apple directory... correct? Empty or what should be inside? - Is it best practice to call
py.test
from the root directory? Orpy.test tests
? - So many projects have a
__init__.py
in their test directory, but that's explicitly said to be wrong in the py.test documentation. So why god why - What comes at the top of the
test_everything.py
file: animport apple
orfrom apple import *
? or something else entirely - Do you call the functions then by
eat()
orapple.eat()
? - Some even recommend manipulating
os.path.dirname
in python
This should be easy, but I've seen every combination of the above, not even speaking about tox and the myriad of other tools. Yet with the slightest error, you get thrown some ImportError: No module named 'apple'
or some other funky error.
What is the "right" way? The advice and the existing code on github etc follows extremely different conventions. For a medium-experienced coder, this should be much easier.