As suggested in the pytest documentation, I have set up my package with the intent not not distributing my tests along with my package:
setup.py
mypkg/
__init__.py
mypkg/
appmodule.py
tests/
test_app.py
...
but I am confused about how to ensure that these tests will run correctly when present (e.g. on Travis CI, or in clones of the project directory).
I want the imports in my test scripts to apply to the source in the adjacent mypkg/ directory (and not to any mypkg that might be installed in site-packages), but I'm getting errors that puzzle me.
If I follow the advice in the pytest documentation and don't put an __init__.py in tests/ then with
from __future__ import absolute_import
from mypkg.appmodule import *
I get
ImportError: No module named mypkg.appmodule
while with
from __future__ import absolute_import
from ..mypkg.appmodule import *
I get
ValueError: Attempted relative import in non-package
And if I ignore the documentation and include an __init__.py, then with the latter I get
ValueError: Attempted relative import beyond top-level package
Only by including an __init__.py and using
from __future__ import absolute_import
from mypkg.appmodule import *
will my tests run. But it isn't clear to me that this will import the source from the adjacent mypkg/ directory if a mypkg is installed in site-packages.
What is the correct way to organize my pytest tests so that they are separate from — and not distributed with — my projects source, but always import the adjacent source, and not version of the package that is installed elswehere?