Home > Software design >  ModuleNotFoundError when pytest tests are in /tests directory in new project
ModuleNotFoundError when pytest tests are in /tests directory in new project

Time:11-19

I am trying to introduce tests early to a Python 3.9 project, however I am having trouble getting going with pytest.

test_legacy.py:

from crmpicco.subtasks.legacy import Legacy

def test_is_complete(self):
    legacy = Legacy()
    assert legacy.is_complete(self) == True

The class I am trying to test is in legacy.py:

from crmpicco.subtasks.subtask import Subtask


class Legacy(Subtask):
    def execute(self):
        print("legacy")

    def is_complete(self):
        return True

when I run this I get:

ImportError while importing test module '/private/var/www/crmpicco/crmpicco/tests/test_legacy.py'. Hint: make sure your test modules/packages have valid Python names.

E ModuleNotFoundError: No module named 'crmpicco'

I have tried adding a __init__.py file to my /tests directory and then removing it again but it has no effect

What am I missing?

CodePudding user response:

pytest tries to find rootdir and probably it things it's /private/var/www/crmpicco/crmpicco The simplest way would be run tests with python -m pytest which run pytest but adds . to PYTHONPATH

CodePudding user response:

What fixed this for me was adding a PYTHONPATH export to my shell.

export PYTHONPATH=/var/www/crmpicco/crmpicco

  • Related