2

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!!!

Gregor
  • 73
  • 4
  • Sinse this is quite a simple question, I'm not going to leave it here. I just need some help. – Gregor May 02 '22 at 20:14

2 Answers2

0

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

project1
  |-__init__.py
  |-module1.py
tests
  |-__init__.py
  |-test1.py
Teejay Bruno
  • 1,716
  • 1
  • 4
  • 11
-1

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

feroz
  • 54
  • 1
  • 7
  • Would appreciate a reason for negative vote, so I could improve. – feroz May 02 '22 at 20:58
  • If you add 'project1' to the path you can `import module1` but not `import project1.module1`. OP wants the second import. – Wombatz May 02 '22 at 21:12
  • actually I tested the code with `project1` in `PYTHONPATH` and it runs the test successfully without complaining about any import error. That's why I came to the above conclusion. Not sure for what reason would it fail? if it can resolve the parent module in the lookup directories. – feroz May 03 '22 at 07:20