pytest-dev / pytest-dev/pytest-django

pytest-django not picking up pytest sys.path auto-config for DJANGO_SETTINGS_MODULE

Open
#441 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

pytest automatically adds the first directory without a __init__.py to the sys.path when discovering tests.

Eg with the following structure:

.
├── AUTHORS
├── CHANGELOG.rst
├── COPYING
├── some_django_app
│   ├── __init__.py
│   ├── management
│   │   ├── commands
│   │   │   └── __init__.py
│   │   └── __init__.py
│   ├── middleware.py
│   └── templatetags
│       └── __init__.py
├── README.rst
├── setup.cfg
├── setup.py
├── tests
│   ├── conftest.py
│   ├── testproject
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   └── urls.py
│   └── test_xyz.py
└── tox.ini

When running tests, "tests/" is added to the sys.path, and in places like conftest.py you can add:

from django.conf import settings

from testproject import settings as testproject_settings


def pytest_configure():
    settings.configure(**vars(testproject_settings))

However it appears as though simply adding DJANGO_SETTINGS_MODULE="testproject.settings" does not work.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect tests/conftest.py and tests/testproject/settings.py, then run tests/test_xyz.py while comparing the working settings.configure path with DJANGO_SETTINGS_MODULE during test discovery. Done means DJANGO_SETTINGS_MODULE loads testproject.settings with the shown layout and the behavior is covered by the relevant test.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.