Skip to content

ModuleNotFoundError when using the tests outside application code layout #2421

@MFry

Description

@MFry
  • Include a detailed description of the bug or suggestion
    I was following the test layout in Good Integration Practices and I couldn't get my tests that were in a similar configuration to run or the simple example I created below.

  • pip list of the virtual environment you are using

  • pytest and operating system versions
    Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-77-generic x86_64)
    platform linux -- Python 3.6.1, pytest-3.0.7, py-1.4.33, pluggy-0.4.0

  • Minimal example if possible

\test_pytest
   mypkg/
       __init__.py
       app.py
   tests/
       test_app.py

and the files

# test_app.py
import app

# tried:
# import mypkg.app
# import ..app
# from mypkg import app
# app.py
print('It Works')
def test_pass():
    assert True == True

Running pytest from the test_pytest directory will result in an error similar to this one.

(py36env) ubuntu@ubuntu-xenial:/vagrant/test_pytest$ pytest

ImportError while importing test module '/vagrant/test_pytest/test/test_app.py'.
Hint: make sure your test modules/packages have valid Python names.
test/test_app.py:1: in <module>
    import app
E   ModuleNotFoundError: No module named 'app'

I can add to the sys path manually by adding a __init__.py to tests/

# test/__init__.py
import sys
sys.path.append('/vagrant/test_pytest/mypkg')

but that will complicate testing with Travis CI

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions