Unittest modules import issue
My project folder structure goes like this:
project/
__init__.py (empty)
tests/
__init__.py (empty)
tests.py
src/
__init__.py
a.py
b.py
c.py
tests/tests.py
import unittest
from a import A
my_a = A()
class TestClass(unittest.TestCase):
# Some tests
pass
src/init.py
__all__ = ["a", "b", "c"]
src/a.py
from b import B
src/b.py
from c import C
src/c.py
pass
Everything works fine when I run the code from the command line but testing it throws this error:
ModuleNotFoundError: No module named ‘b’
I did lots of research that I could condensed to these links:
- Python unittest failing to resolve import statements
- Running unittest with typical test directory structure
- How do I write good/correct package __init__.py files
- https://docs.python.org/3/tutorial/modules.html#packages
Right now I’m really confused about how __init__.py
would work in this case and why my test can’t find imported modules path. Can anyone lend me a hand with this please?
3
from .b import B
from .c import C
Ahmad Umaev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2