0

I'm using Python 3.7.5 and pytest 7.0.1 on Linux. I am writing unit test cases of my project in /tests directory and executing it by following command parent directory, it ran successfully.

python3 -m pytest tests/xxx/test_xxx.py

However, when i copy the same script in /pytest directory and ran the same script again, it ended up with an error.

python3 -m pytest pytests/xxx/test_xxx.py

Error:

ImportError while importing test module
'/projects/db/analytics/advanced_analytics/advanced_analytics/pytests/xxx/test_xxx.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback: /opt/python3.7/lib/python3.7/importlib/__init__.py:127: in
import_module
return _bootstrap._gcd_import(name[level:], package, level) pytests/xxx/test_xxx.py:21: in <module>
from myproject.my_project import my_project my_project/my_project.py:13: in <module>
from advanced_analytics.framework.CICLogger import CICLogger  E   ModuleNotFoundError: No module named 'advanced_analytics.framework'

Please note my_project , /tests and /pytests are created in same parent directory - advanced_analytics.

from advanced_analytics.framework.CICLogger import CICLogger imported in my_project.py file

Kindly let me know why this issue is occurring and how to fix the issue.

  • I solve this problem in my own projects by creating a `setup.py` and installing the module before running unit tests. See https://stackoverflow.com/a/69228512/3216427 – joanis Mar 08 '22 at 18:15
  • But see also https://stackoverflow.com/q/58101478/3216427 (another way to import local modules for unit testing) and https://stackoverflow.com/a/56791072/3216427 (maybe you didn't create your `__init__.py` file yet?) – joanis Mar 08 '22 at 18:19

0 Answers0