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 1 commit
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
Prev Previous commit
Next Next commit
Move logic to determine IS_RUNNING_TESTS to a function
This makes the logic testable.
  • Loading branch information
tim-schilling committed May 17, 2025
commit 8fbdf1cbf460822dc872c588c7034db0a3444523
11 changes: 10 additions & 1 deletion debug_toolbar/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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,
}

Expand Down
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())
Loading