Skip to content

Added check for pytest as test runner for IS_RUNNING_TESTS. #2137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion debug_toolbar/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import sys
import warnings
from functools import cache
Expand All @@ -6,6 +7,15 @@
from django.dispatch import receiver
from django.test.signals import setting_changed


def _is_running_tests():
"""
Helper function to support testing default value for
IS_RUNNING_TESTS
"""
return "test" in sys.argv or "PYTEST_VERSION" in os.environ


CONFIG_DEFAULTS = {
# Toolbar options
"DISABLE_PANELS": {
Expand Down Expand Up @@ -43,7 +53,7 @@
"SQL_WARNING_THRESHOLD": 500, # milliseconds
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
"TOOLBAR_LANGUAGE": None,
"IS_RUNNING_TESTS": "test" in sys.argv,
"IS_RUNNING_TESTS": _is_running_tests(),
"UPDATE_ON_FETCH": False,
}

Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change log
Pending
-------

* Added support for checking if pytest as the test runner when determining
if tests are running.

5.2.0 (2025-04-29)
------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Toolbar options

* ``IS_RUNNING_TESTS``

Default: ``"test" in sys.argv``
Default: ``"test" in sys.argv or "PYTEST_VERSION" in os.environ``

This setting whether the application is running tests. If this resolves to
``True``, the toolbar will prevent you from running tests. This should only
Expand Down
2 changes: 1 addition & 1 deletion docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ can do this by adding another setting:

.. code-block:: python
TESTING = "test" in sys.argv
TESTING = "test" in sys.argv or "PYTEST_VERSION" in os.environ
if not TESTING:
INSTALLED_APPS = [
Expand Down
2 changes: 2 additions & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ psycopg
py
pyflame
pylibmc
pytest
pyupgrade
querysets
refactoring
Expand All @@ -65,5 +66,6 @@ theming
timeline
tox
uWSGI
unhandled
unhashable
validator
22 changes: 22 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from unittest.mock import patch

from django.test import TestCase

from debug_toolbar.settings import _is_running_tests


class SettingsTestCase(TestCase):
@patch("debug_toolbar.settings.sys")
@patch("debug_toolbar.settings.os")
def test_is_running_tests(self, mock_os, mock_sys):
mock_sys.argv = "test"
mock_os.environ = {}
self.assertTrue(_is_running_tests())

mock_sys.argv = ""
mock_os.environ = {}
self.assertFalse(_is_running_tests())

mock_sys.argv = ""
mock_os.environ = {"PYTEST_VERSION": "1"}
self.assertTrue(_is_running_tests())