File tree Expand file tree Collapse file tree 6 files changed +40
-3
lines changed Expand file tree Collapse file tree 6 files changed +40
-3
lines changed Original file line number Diff line number Diff line change 1+ import os
12import sys
23import warnings
34from functools import cache
67from django .dispatch import receiver
78from django .test .signals import setting_changed
89
10+
11+ def _is_running_tests ():
12+ """
13+ Helper function to support testing default value for
14+ IS_RUNNING_TESTS
15+ """
16+ return "test" in sys .argv or "PYTEST_VERSION" in os .environ
17+
18+
919CONFIG_DEFAULTS = {
1020 # Toolbar options
1121 "DISABLE_PANELS" : {
4353 "SQL_WARNING_THRESHOLD" : 500 , # milliseconds
4454 "OBSERVE_REQUEST_CALLBACK" : "debug_toolbar.toolbar.observe_request" ,
4555 "TOOLBAR_LANGUAGE" : None ,
46- "IS_RUNNING_TESTS" : "test" in sys . argv ,
56+ "IS_RUNNING_TESTS" : _is_running_tests () ,
4757 "UPDATE_ON_FETCH" : False ,
4858}
4959
Original file line number Diff line number Diff line change @@ -4,6 +4,9 @@ Change log
44Pending
55-------
66
7+ * Added support for checking if pytest as the test runner when determining
8+ if tests are running.
9+
7105.2.0 (2025-04-29)
811------------------
912
Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ Toolbar options
7777
7878* ``IS_RUNNING_TESTS ``
7979
80- Default: ``"test" in sys.argv ``
80+ Default: ``"test" in sys.argv or "PYTEST_VERSION" in os.environ ``
8181
8282 This setting whether the application is running tests. If this resolves to
8383 ``True ``, the toolbar will prevent you from running tests. This should only
Original file line number Diff line number Diff line change @@ -165,7 +165,7 @@ can do this by adding another setting:
165165
166166.. code-block :: python
167167
168- TESTING = " test" in sys.argv
168+ TESTING = " test" in sys.argv or " PYTEST_VERSION " in os.environ
169169
170170 if not TESTING :
171171 INSTALLED_APPS = [
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ psycopg
4949py
5050pyflame
5151pylibmc
52+ pytest
5253pyupgrade
5354querysets
5455refactoring
@@ -65,5 +66,6 @@ theming
6566timeline
6667tox
6768uWSGI
69+ unhandled
6870unhashable
6971validator
Original file line number Diff line number Diff line change 1+ from unittest .mock import patch
2+
3+ from django .test import TestCase
4+
5+ from debug_toolbar .settings import _is_running_tests
6+
7+
8+ class SettingsTestCase (TestCase ):
9+ @patch ("debug_toolbar.settings.sys" )
10+ @patch ("debug_toolbar.settings.os" )
11+ def test_is_running_tests (self , mock_os , mock_sys ):
12+ mock_sys .argv = "test"
13+ mock_os .environ = {}
14+ self .assertTrue (_is_running_tests ())
15+
16+ mock_sys .argv = ""
17+ mock_os .environ = {}
18+ self .assertFalse (_is_running_tests ())
19+
20+ mock_sys .argv = ""
21+ mock_os .environ = {"PYTEST_VERSION" : "1" }
22+ self .assertTrue (_is_running_tests ())
You can’t perform that action at this time.
0 commit comments