Skip to content

Instantly share code, notes, and snippets.

@pelme
Created August 1, 2014 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pelme/6815b40ba294df6d9679 to your computer and use it in GitHub Desktop.
Save pelme/6815b40ba294df6d9679 to your computer and use it in GitHub Desktop.
$ 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 ===============================================
@Suor
Copy link

Suor commented Aug 1, 2014

You forgot touch tests/__init__.py.

@pelme
Copy link
Author

pelme commented Aug 4, 2014

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