I have the following package layout (as recommended here):
module/
lib/
__init__.py
module.py
util.py
tests/
env.py
test_util.py
The file util.py
contains a utility function f()
which is re-used multiple times within the package. It's not exposed to users, since it doesn't make sense outside the context of the package, but it does make sense to write a unit test for it.
The file env.py
contains these lines:
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
How do I set up my package to that the unit tests have access to util.f
?
Update: After some back-and-forth in the comments and some testing it seems that adding the lib
directory to the system path as well by adding:
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../lib')))
to env.py
does the trick. However, this seems to break relative imports. I can live without relative imports for now, but is this a standard/robust way of setting up unit tests, or is this going to break in the long term?