0

I am having problems with pytest my directory looks like below:

├── modules
|   ├── __init__.py
│   ├── data
|   |   ├── __init__.py
│   |   └── tables.py
├── tests
│   └── test_tabels.py
└──  main.py

My problem was that I was getting a Pytest Discovery Error. When reading the error output I get ModuleNotFoundError: No module named modules. In my test_tables.py file I obviously need to import classes and definitions to test them, so I would for instance run from modules.data import GetData.

By adding a __init__.py file to the tests folder I am not getting the error anymore and can run my tests. However, I do not understand why this works. I have other repos where I do not have an __init__.py file in the tests folder where I do not have the issue. This makes me wonder if I have actually solved the problem and if it makes sense to add the __init__.py file.

andKaae
  • 173
  • 1
  • 13
  • Can you try running `pytest --collect-only` and post any error you get? – alec_djinn Sep 01 '23 at 12:33
  • Make sure to use `python -m pytest tests` when you run your tests, using the `-m` flag is important so your project root is added to the path. https://docs.pytest.org/en/7.1.x/how-to/usage.html#calling-pytest-through-python-m-pytest – Tzane Sep 01 '23 at 12:39
  • Does this answer your question? [What is \_\_init\_\_.py for?](https://stackoverflow.com/questions/448271/what-is-init-py-for) – Sam Sep 01 '23 at 12:40

0 Answers0