'Why can't pytest-django find manage.py?

I have a project structure like this:

setup.cfg
integration_tests/
  |----tests.py
src/
  |----manage.py
  |----my_django_app/

And a setup.cfg with this:

[tool:pytest]
addopts=--tb=short --strict -ra
DJANGO_SETTINGS_MODULE = my_django_app.settings
env = 
    APP_ENV=testing
    USE_SQLITE_DB=True

But when I navigate to the top of that directory structure and run pytest, I get the following:

"pytest-django could not find a Django project (no manage.py file could be found). You must explicitly add your Django project to the Python path to have it picked up."

If I instead navigate to /src/ and run pytest, the tests run without issue, though obviously since I am down a level none of the inegration tests run.

According to the documentation, it seems like it should drill down from where you run pytest to attempt to find a manage.py: https://pytest-django.readthedocs.io/en/latest/managing_python_path.html#automatic-looking-for-of-django-projects

If that isn't the case, then is there some way to configure a setting in setup.cfg to add src/ to the PYTHONPATH? The suggestion in the doc to install src/ in editable mode using pip isn't really tenable in our environment.



Solution 1:[1]

You can use pytest-pythonpaths plugin for pytests.

Example:

[pytest] 
python_paths = your/path/apps your/path/libs

In your specific case:

[tool:pytest]
addopts=--tb=short --strict -ra
DJANGO_SETTINGS_MODULE = my_django_app.settings
env = 
    APP_ENV=testing
    USE_SQLITE_DB=True
python_paths = src

Why manage.py can't be found?

First take a look at how does pytest test discovery.

Conventions for Python test discovery

pytest implements the following standard test discovery:

  • If no arguments are specified then collection starts from testpaths (if configured) or the current directory. Alternatively, command line arguments can be used in any combination of directories, file names or node ids.

  • Recurse into directories, unless they match norecursedirs. In those directories, search for test_*.py or *_test.py files, imported by their test package name.

  • From those files, collect test items: test prefixed test functions or methods outside of class test prefixed test functions or methods inside Test prefixed test classes (without an init method)

So, manage.py do not qualify as a test file hence is not discovered automatically thought, you need it in the python path in order to run your tests.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1