How to solve PytestConfigWarning: Unknown config option: DJANGO_ SETTINGS_MODULE error?
Asked Answered
U

6

17

I am using django to build my website and I have used django-pytest to test my apps but I have got this error Note I am usign python 3.9

================================================================== warnings summary 

===================================================================
..\venv\lib\site-packages\_pytest\config\__init__.py:1233
  c:\users\eng_diaa_shalaby\desktop\unittest\venv\lib\site-packages\_pytest\config\__init__.py:1233: PytestConfigWarning: Unknown config option: DJANGO_
SETTINGS_MODULE

    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/warnings.html

This is my pytest.ini file content

# -- FILE: pytest.ini (or tox.ini)
[pytest]
DJANGO_SETTINGS_MODULE = testing_settings
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py

and I run this command

pytest

and this is my venv packages

Package       Version
------------- -------
asgiref       3.3.4
atomicwrites  1.4.0
attrs         21.2.0
colorama      0.4.4
coverage      5.5
Django        3.2.4
django-pytest 0.2.0
iniconfig     1.1.1
packaging     20.9
pip           21.1.2
pluggy        0.13.1
py            1.10.0
pyparsing     2.4.7
pytest        6.2.4
pytz          2021.1
setuptools    57.0.0
sqlparse      0.4.1
toml          0.10.2
Upholsterer answered 11/6, 2021 at 12:13 Comment(0)
U
15

I found out what was the reason for my problem. I just uninstall

django-pytest 0.2.0

and install

pytest-django

instead by running the following command

pip install pytest-django

and run the following command after pip installed pytest-django

set DJANGO_SETTINGS_MODULE=testing.settings

And everything just works fine.

Upholsterer answered 24/6, 2021 at 8:27 Comment(1)
Hm. django-pytest/pytest-django is not the problem. It seems that pytest-django does not respect the DJANGO_SETTINGS_MODULE line in pytest.ini, despite the docs say so...Etter
L
13

The accepted answer it's not answering OP question neither providing solution.

I have encountered the same problem, I'm using Django 4.0 and Pytest 7.2.1

I'm using pytest.ini as configuration file.

Solution:

[tool:pytest]
DJANGO_SETTINGS_MODULE = core.settings.local
python_files = test_*.py

Adding keyword tool solves the problem.

Another thing that might be necessary is adding ENV variable:

export DJANGO_SETTINGS_MODULE=mysite.settings 
Lilylivered answered 7/2, 2023 at 5:22 Comment(2)
This just ignores the pytest sectionUniversalism
adding tool fixed it for me, thanks!Lucubrate
F
4

The warning

PytestConfigWarning: Unknown config option: some-name

means that the config option some-name is not known to pytest or any of the currently installed pytest plugins (just what the warning implies). In particular, here it means that either pytest-django is not installed, or it is not visible to pytest for some reason (e.g. you have installed pytest-django using the wrong pip command).

To troubleshoot, first list all installed plugins that pytest finds using pytest -VV:

$ pytest -VV
This is pytest version 6.2.5, imported from /private/tmp/so/lib/python3.9/site-packages/pytest/__init__.py
setuptools registered plugins:
  pytest-django-4.5.2 at /private/tmp/so/lib/python3.9/site-packages/pytest_django/plugin.py
  pytest-cov-3.0.0 at /private/tmp/so/lib/python3.9/site-packages/pytest_cov/plugin.py

Here, pytest reports that two plugins are found: pytest-cov and pytest-django.

Now, if pytest-django is not listed, check where it was installed by e.g. issuing pip show pytest-django and looking for the path under Location:

$ pip show pytest-django
...
Location: /private/tmp/so/lib/python3.9/site-packages

Or, if pip is not available for you, check where Python import pytest_django package from via

$ python -c 'import pytest_django; print(pytest_django.__path__[0])'
/private/tmp/so/lib/python3.9/site-packages/pytest_django

The paths to site-packages directory in both outputs should match. In this example, both pytest -VV and pip show pytest-django report /private/tmp/so/lib/python3.9/site-packages as the installation path. If those differ, reinstall pytest-django using the correct Python environment.

Frankhouse answered 18/1, 2022 at 11:10 Comment(0)
F
1

You should correct your pytest.ini file by removing spaces around =:

[pytest]
DJANGO_SETTINGS_MODULE=your_project_name.settings

Note: per Django structure, your project should have a subfolder with settings.py file in it. You need to point pytest to this file and use dot . instead of /.

Formation answered 2/1, 2022 at 4:50 Comment(0)
M
0

Well, In my case I was having an entry py.test --ignore=outofscope in my pytest.ini file, which was causing that warning.

I solved my problem by updating my pytest.ini file.

  • look for any warning/error raised by your editor
  • remove any extra space when assigning some values
Martel answered 16/2, 2023 at 15:14 Comment(0)
U
0

In my case, for some reason pytest was not runnin from the currently activated venv

Universalism answered 28/11, 2023 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.