0

I use pytest. I have a "kernel" folder in the root of the project in which the file is located "ecommerce.py" Full path "./kernel/ecommerce.py" I have a unit test that should test this file it is in the path: "./test/test_ecommerce.py" How do I correctly specify the import of a file "ecommerce.py" in the test file?

project_root/
    ├── kernel/
    │   └── ecommerce.py
    └── test/
        └── test_ecommerce.py

My attempts boiled down to the following:

from ..kernel.ecommerce import Ecommerce

and

from kernel.ecommerce import Ecommerce

and

os.path.dirname('/full/path/root_dir_project')
from kernel.ecommerce import Ecommerce

Despite the fact that the IDE sees the module correctly, py test itself cannot determine the kernel

ImportError while importing test module '/full/path/root_dir_project/kernel/test/test_ecommerce.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
kernel/test/test_ecommerce.py:7: in <module>
    from ..kernel.ecommerce import Ecommerce
E   ImportError: attempted relative import with no known parent package
stas stas
  • 95
  • 10
  • Thanks! - oddly enough, the advice on the link worked after several attempts! – stas stas Jul 16 '23 at 08:43
  • There are a few ways of arranging your code and tests and it can take a bit of coercion to get everything working together. Generally worth the up front investment to get things running conveniently so they keep getting used. – Sam Mason Jul 16 '23 at 18:17

0 Answers0