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
1
2
import sys
2
3
import warnings
3
4
from functools import cache
6
7
from django .dispatch import receiver
7
8
from django .test .signals import setting_changed
8
9
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
+
9
19
CONFIG_DEFAULTS = {
10
20
# Toolbar options
11
21
"DISABLE_PANELS" : {
43
53
"SQL_WARNING_THRESHOLD" : 500 , # milliseconds
44
54
"OBSERVE_REQUEST_CALLBACK" : "debug_toolbar.toolbar.observe_request" ,
45
55
"TOOLBAR_LANGUAGE" : None ,
46
- "IS_RUNNING_TESTS" : "test" in sys . argv ,
56
+ "IS_RUNNING_TESTS" : _is_running_tests () ,
47
57
"UPDATE_ON_FETCH" : False ,
48
58
}
49
59
Original file line number Diff line number Diff line change @@ -4,6 +4,9 @@ Change log
4
4
Pending
5
5
-------
6
6
7
+ * Added support for checking if pytest as the test runner when determining
8
+ if tests are running.
9
+
7
10
5.2.0 (2025-04-29)
8
11
------------------
9
12
Original file line number Diff line number Diff line change @@ -77,7 +77,7 @@ Toolbar options
77
77
78
78
* ``IS_RUNNING_TESTS ``
79
79
80
- Default: ``"test" in sys.argv ``
80
+ Default: ``"test" in sys.argv or "PYTEST_VERSION" in os.environ ``
81
81
82
82
This setting whether the application is running tests. If this resolves to
83
83
``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:
165
165
166
166
.. code-block :: python
167
167
168
- TESTING = " test" in sys.argv
168
+ TESTING = " test" in sys.argv or " PYTEST_VERSION " in os.environ
169
169
170
170
if not TESTING :
171
171
INSTALLED_APPS = [
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ psycopg
49
49
py
50
50
pyflame
51
51
pylibmc
52
+ pytest
52
53
pyupgrade
53
54
querysets
54
55
refactoring
@@ -65,5 +66,6 @@ theming
65
66
timeline
66
67
tox
67
68
uWSGI
69
+ unhandled
68
70
unhashable
69
71
validator
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