pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.37k stars 344 forks source link

DEBUG setting inconsistency #915

Closed jpic closed 3 years ago

jpic commented 3 years ago

Running pytest-django on a normal project, which includes a URL if settings.DEBUG as such:

if settings.DEBUG:
    urlpatterns.append(
        path('bundles/', include('ryzom_django.bundle')),
    )

The problem is that when urls.py is imported, settings.DEBUG is False, but it shouldn't:

> /home/jpic/src/electeez/electeez/urls.py(20)<module>()
-> if settings.DEBUG:
(Pdb) settings.DEBUG
False
(Pdb) import os
(Pdb) os.environ['DJANGO_SETTINGS_MODULE']
'electeez.settings'
(Pdb) from electeez import settings
(Pdb) settings.DEBUG
True

As such, my url is not defined and my tests fail, because during the test settings.DEBUG is true.

jpic commented 3 years ago

All right, didn't know the django_debug_mode was there to help, sorry for the noise! Keep up the great work

:tophat:

jpic commented 3 years ago

Actually, I should ask my question the other way around. If DEBUG is False, my code also works, the problem occurs when part of the code has DEBUG=True and the other has DEBUG=False, which is the case here: DEBUG is not overriden by pytest during App.ready()

$ py.test -sv
> /home/jpic/src/electeez/electeez/settings.py(26)<module>()
-> REGISTRATION_OPEN = True
(Pdb) c
> /home/jpic/src/ryzom/src/ryzom_django/bundle.py(15)<module>()
-> CSS_BUNDLE_URL = reverse_lazy('bundle_css')
(Pdb) l
 10     from ryzom import html
 11     
 12     
 13     if settings.DEBUG:
 14         breakpoint()
 15  ->     CSS_BUNDLE_URL = reverse_lazy('bundle_css')
 16         JS_BUNDLE_URL = reverse_lazy('bundle_js')
 17     else:
 18         CSS_BUNDLE_URL = staticfiles_storage.url('bundle.css')
 19         JS_BUNDLE_URL = staticfiles_storage.url('bundle.js')
 20     
(Pdb) bt
  /usr/bin/py.test(33)<module>()
-> sys.exit(load_entry_point('pytest==6.2.1', 'console_scripts', 'py.test')())
  /usr/lib/python3.9/site-packages/_pytest/config/__init__.py(185)console_main()
-> code = main()
  /usr/lib/python3.9/site-packages/_pytest/config/__init__.py(143)main()
-> config = _prepareconfig(args, plugins)
  /usr/lib/python3.9/site-packages/_pytest/config/__init__.py(318)_prepareconfig()
-> config = pluginmanager.hook.pytest_cmdline_parse(
  /usr/lib/python3.9/site-packages/pluggy/hooks.py(286)__call__()
-> return self._hookexec(self, self.get_hookimpls(), kwargs)
  /usr/lib/python3.9/site-packages/pluggy/manager.py(93)_hookexec()
-> return self._inner_hookexec(hook, methods, kwargs)
  /usr/lib/python3.9/site-packages/pluggy/manager.py(84)<lambda>()
-> self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  /usr/lib/python3.9/site-packages/pluggy/callers.py(187)_multicall()
-> res = hook_impl.function(*args)
  /usr/lib/python3.9/site-packages/_pytest/config/__init__.py(1003)pytest_cmdline_parse()
-> self.parse(args)
  /usr/lib/python3.9/site-packages/_pytest/config/__init__.py(1283)parse()
-> self._preparse(args, addopts=addopts)
  /usr/lib/python3.9/site-packages/_pytest/config/__init__.py(1191)_preparse()
-> self.hook.pytest_load_initial_conftests(
  /usr/lib/python3.9/site-packages/pluggy/hooks.py(286)__call__()
-> return self._hookexec(self, self.get_hookimpls(), kwargs)
  /usr/lib/python3.9/site-packages/pluggy/manager.py(93)_hookexec()
-> return self._inner_hookexec(hook, methods, kwargs)
  /usr/lib/python3.9/site-packages/pluggy/manager.py(84)<lambda>()
-> self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  /usr/lib/python3.9/site-packages/pluggy/callers.py(187)_multicall()
-> res = hook_impl.function(*args)
  /home/jpic/.local/lib/python3.9/site-packages/pytest_django/plugin.py(322)pytest_load_initial_conftests()
-> _setup_django()
  /home/jpic/.local/lib/python3.9/site-packages/pytest_django/plugin.py(224)_setup_django()
-> django.setup()
  /home/jpic/.local/lib/python3.9/site-packages/django/__init__.py(24)setup()
-> apps.populate(settings.INSTALLED_APPS)
  /home/jpic/.local/lib/python3.9/site-packages/django/apps/registry.py(122)populate()
-> app_config.ready()
  /home/jpic/src/ryzom/src/ryzom_django/apps.py(9)ready()
-> autodiscover_modules('components')
  /home/jpic/.local/lib/python3.9/site-packages/django/utils/module_loading.py(47)autodiscover_modules()
-> import_module('%s.%s' % (app_config.name, module_to_search))
  /usr/lib/python3.9/importlib/__init__.py(127)import_module()
-> return _bootstrap._gcd_import(name[level:], package, level)
  <frozen importlib._bootstrap>(1030)_gcd_import()
  <frozen importlib._bootstrap>(1007)_find_and_load()
  <frozen importlib._bootstrap>(986)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(680)_load_unlocked()
  <frozen importlib._bootstrap_external>(790)exec_module()
  <frozen importlib._bootstrap>(228)_call_with_frames_removed()
  /home/jpic/src/electeez/electeez/components.py(4)<module>()
-> from ryzom_django_mdc import html
  <frozen importlib._bootstrap>(1058)_handle_fromlist()
  <frozen importlib._bootstrap>(228)_call_with_frames_removed()
  <frozen importlib._bootstrap>(1007)_find_and_load()
  <frozen importlib._bootstrap>(986)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(680)_load_unlocked()
  <frozen importlib._bootstrap_external>(790)exec_module()
  <frozen importlib._bootstrap>(228)_call_with_frames_removed()
  /home/jpic/src/ryzom/src/ryzom_django_mdc/html.py(4)<module>()
-> from ryzom_django import html as ryzom_django
  <frozen importlib._bootstrap>(1058)_handle_fromlist()
  <frozen importlib._bootstrap>(228)_call_with_frames_removed()
  <frozen importlib._bootstrap>(1007)_find_and_load()
  <frozen importlib._bootstrap>(986)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(680)_load_unlocked()
  <frozen importlib._bootstrap_external>(790)exec_module()
  <frozen importlib._bootstrap>(228)_call_with_frames_removed()
  /home/jpic/src/ryzom/src/ryzom_django/html.py(4)<module>()
-> from .bundle import CSS_BUNDLE_URL, JS_BUNDLE_URL
  <frozen importlib._bootstrap>(1007)_find_and_load()
  <frozen importlib._bootstrap>(986)_find_and_load_unlocked()
  <frozen importlib._bootstrap>(680)_load_unlocked()
  <frozen importlib._bootstrap_external>(790)exec_module()
  <frozen importlib._bootstrap>(228)_call_with_frames_removed()
> /home/jpic/src/ryzom/src/ryzom_django/bundle.py(15)<module>()
-> CSS_BUNDLE_URL = reverse_lazy('bundle_css')

True or False, I'm fine with any, but shouldn't that setting be consistent? (or removed, I don't see why would pytest-django temper with settings by default ...)

bluetech commented 3 years ago

Did you try django_debug_mode = keep?

bluetech commented 3 years ago

I'll close as fixed by keep, but feel free to reopen with more details.