From 357b3ea53171af431cc10d4f8b4376cfda528c4f Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Wed, 14 May 2025 18:44:36 -0500 Subject: [PATCH 1/3] Added check for pytest as test runner for IS_RUNNING_TESTS. --- debug_toolbar/settings.py | 3 ++- docs/changes.rst | 3 +++ docs/configuration.rst | 2 +- docs/installation.rst | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index 59d538a0b..648d51bb2 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -1,3 +1,4 @@ +import os import sys import warnings from functools import cache @@ -43,7 +44,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": "test" in sys.argv or "PYTEST_VERSION" in os.environ, "UPDATE_ON_FETCH": False, } diff --git a/docs/changes.rst b/docs/changes.rst index bf1998de8..6d6f34b2d 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -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) ------------------ diff --git a/docs/configuration.rst b/docs/configuration.rst index d9e7ff342..377c97da8 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -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 diff --git a/docs/installation.rst b/docs/installation.rst index 61187570d..b89a2f563 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -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 = [ From 8fbdf1cbf460822dc872c588c7034db0a3444523 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 16 May 2025 16:09:18 -0500 Subject: [PATCH 2/3] Move logic to determine IS_RUNNING_TESTS to a function This makes the logic testable. --- debug_toolbar/settings.py | 11 ++++++++++- tests/test_settings.py | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/test_settings.py diff --git a/debug_toolbar/settings.py b/debug_toolbar/settings.py index 648d51bb2..4dc801c2c 100644 --- a/debug_toolbar/settings.py +++ b/debug_toolbar/settings.py @@ -7,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": { @@ -44,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 or "PYTEST_VERSION" in os.environ, + "IS_RUNNING_TESTS": _is_running_tests(), "UPDATE_ON_FETCH": False, } diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 000000000..f7dc676b1 --- /dev/null +++ b/tests/test_settings.py @@ -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()) From 4438cbe9e271354b020f2db1046376af08a7b6ff Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Sat, 17 May 2025 07:30:29 -0500 Subject: [PATCH 3/3] Added words to spelling list for the changelog. --- docs/spelling_wordlist.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index 0f58c1f52..79b05cb06 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -49,6 +49,7 @@ psycopg py pyflame pylibmc +pytest pyupgrade querysets refactoring @@ -65,5 +66,6 @@ theming timeline tox uWSGI +unhandled unhashable validator