-
-
Save pelme/6815b40ba294df6d9679 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ mkdir mypkg tests | |
$ touch mypkg/__init__.py | |
$ echo 'bar = 1' > mypkg/foo.py | |
$ cat > tests/test_a.py | |
from mypkg.foo import bar | |
def test_a(): | |
assert bar == 1 | |
$ py.test | |
================================================= test session starts ================================================== | |
platform darwin -- Python 3.3.4 -- py-1.4.20 -- pytest-2.5.2 | |
collected 0 items / 1 errors | |
======================================================== ERRORS ======================================================== | |
___________________________________________ ERROR collecting tests/test_a.py ___________________________________________ | |
tests/test_a.py:1: in <module> | |
> from mypkg.foo import bar | |
E ImportError: No module named 'mypkg' | |
=============================================== 1 error in 0.01 seconds ================================================ | |
$ PYTHONPATH=. py.test | |
================================================= test session starts ================================================== | |
platform darwin -- Python 3.3.4 -- py-1.4.20 -- pytest-2.5.2 | |
collected 1 items | |
tests/test_a.py . | |
=============================================== 1 passed in 0.01 seconds =============================================== |
I left it out by choice: http://pytest.org/latest/goodpractises.html#choosing-a-test-layout-import-rules
What happens when __init__.py
is present is that pytest adds its parent directory to the path, which also makes mypkg importable. This happens during test collection - therefore, mypkg is not importable at setup time, which is the place where Django apps is loaded.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You forgot
touch tests/__init__.py
.