Home > Software engineering >  Testing project using pytest with tests from external "tests" directory
Testing project using pytest with tests from external "tests" directory

Time:05-03

I have a problem with that testing. I tried to find anything but all guides are about using pycharm's abilities. So, I have a structure like this:

project1
|-module1.py
tests
|-test1.py

module1.py:

def foo(a, b):
    return a   b   1

test_module1.py:

from project1.module1 import foo

def test_foo():
    assert foo(1, 1) == 3

While I stay in root directory (where this structure is located), how can i run testing with pytest from terminal. I really tried my best, but always was getting different errors. Most of them look like ModuleNotFoundError: No module named 'project1'. Thanks!!!

CodePudding user response:

You need to add an empty __init__.py into each sub directory.

project1
  |-__init__.py
  |-module1.py
tests
  |-__init__.py
  |-test1.py

CodePudding user response:

You need to add project1 path into your PYTHONPATH

OR

use inline command: PYTHONPATH=project1 pytest tests/

This thread might help clarify your doubt: https://stackoverflow.com/a/15317038/2022338

  • Related