0

The setting is the following

/project/src/package/module.py
/project/tests/package/test_module.py

In test_module.py I have an import from the src branch like so:

from package.module import SomeClass

Finally, I try to invoke the following command from /project/

python3 -m pytest tests

And I get the following error message:

ModuleNotFoundError: No module named 'package.module'

I cannot get it working. And I have difficulties to understand how 'pytest' would even have a chance to find the correct modules, since they are in the 'src' directory and the imports start from the package layer. I am confused.

CodingButStillAlive
  • 733
  • 2
  • 8
  • 22

2 Answers2

1

As suggested in one of the comments, one way of dealing with this is adding a conftest.py file to the src directory.

The file doesn't need to contain anything.

Your file tree would look like then like this:

/project/src/package/module.py
/project/src/conftest.py
/project/tests/package/test_module.py

Once you've added conftest.py, you should be able to run pytest from /project/.

Paul P
  • 3,346
  • 2
  • 12
  • 26
0

Thank you for your combined help.

It turns out to be a mixture of several things.

1.) By writing to a log file, I could confirm that you need to put conftest.py into the src directory in order to get it appended to the path.

2.) There should be no __init__.py files in the /project/tests/ subdirectories that have equal names to the subdirectories under /project/src. This is because the /tests/ subdirectories come before the /src/ subdirectories in the list of sys.path. Thus, pytest will recognize directories under /tests first - and if they have an __init__.py file, assume they are the modules/packages you are looking for. Maybe renaming those directories to "tests/test_module" instead of "tests/module" might help here. Note that this especially applies to the directory in which you have your test_xy.py files.

3.) Finally, you should not use python3 -m pytest tests but python3 -m pytest instead, so that the current directory is the starting point, giving pytest the chance to locate conftest.py in the src directory.

Maybe it makes even sense to not have the same names for packages under tests like under source if - for some reason - you need __init__.py files in them. You could call them test_package, for example.

Honestly, I do wonder why this has never been investigated. As you pointed out, there have been tons of discussions about that. Everyone contributed a little bit. But none was comprehensive. So how do people work in practice? I mean we speak of testing essentials after all.

In all fairness, I also learned from the documentation of pytest that you should build your package properly with a setup.py file instead.

Wow, what a journey. I hope others may benefit. And I thank you all for helping me out.

CodingButStillAlive
  • 733
  • 2
  • 8
  • 22
  • As mentioned in the answer I linked, the standard way is to use `setup.py` with the package, so this is the way most people use. The `conftest` hack is nice, but still a hack IMHO. – MrBean Bremen Jun 19 '21 at 20:52