6
6
import memory_profiler
7
7
import pytest
8
8
9
+ from pytest_monitor .models import PyTestMonitorConfig
9
10
from pytest_monitor .session import PyTestMonitorSession
10
11
11
12
# These dictionaries are used to compute members set on each items.
23
24
}
24
25
PYTEST_MONITOR_DEPRECATED_MARKERS = {}
25
26
26
- PYTEST_MONITORING_ENABLED = True
27
+ config_stash = pytest . StashKey [ PyTestMonitorConfig ]()
27
28
28
29
29
30
def pytest_addoption (parser ):
@@ -117,13 +118,13 @@ def pytest_configure(config):
117
118
)
118
119
119
120
120
- def pytest_runtest_setup (item ):
121
+ def pytest_runtest_setup (item : pytest . Item ):
121
122
"""
122
123
Validate marker setup and print warnings if usage of deprecated marker is identified.
123
124
Setting marker attribute to the discovered item is done after the above described verification.
124
125
:param item: Test item
125
126
"""
126
- if not PYTEST_MONITORING_ENABLED :
127
+ if not item . session . stash [ config_stash ]. enabled :
127
128
return
128
129
item_markers = {mark .name : mark for mark in item .iter_markers () if mark and mark .name .startswith ("monitor_" )}
129
130
mark_to_del = []
@@ -158,7 +159,7 @@ def pytest_runtest_setup(item):
158
159
159
160
160
161
@pytest .hookimpl (tryfirst = True , hookwrapper = True )
161
- def pytest_runtest_makereport (item , call ):
162
+ def pytest_runtest_makereport (item : pytest . Item , call : pytest . CallInfo ):
162
163
"""
163
164
Used to identify the current call to add times.
164
165
:param item: Test item
@@ -172,8 +173,8 @@ def pytest_runtest_makereport(item, call):
172
173
setattr (item , "test_effective_start_time" , call .start )
173
174
174
175
175
- def pytest_runtest_call (item ):
176
- if not PYTEST_MONITORING_ENABLED :
176
+ def pytest_runtest_call (item : pytest . Item ):
177
+ if not item . session . stash [ config_stash ]. enabled :
177
178
return
178
179
setattr (item , "monitor_results" , False )
179
180
if hasattr (item , "module" ):
@@ -187,7 +188,7 @@ def pytest_runtest_call(item):
187
188
188
189
189
190
@pytest .hookimpl
190
- def pytest_pyfunc_call (pyfuncitem ):
191
+ def pytest_pyfunc_call (pyfuncitem : pytest . Function ):
191
192
"""
192
193
Core sniffer logic. We encapsulate the test function in a sniffer function to collect
193
194
memory results.
@@ -211,7 +212,7 @@ def prof():
211
212
setattr (pyfuncitem , "mem_usage" , memuse )
212
213
setattr (pyfuncitem , "monitor_results" , True )
213
214
214
- if not PYTEST_MONITORING_ENABLED :
215
+ if not pyfuncitem . session . stash [ config_stash ]. enabled :
215
216
wrapped_function ()
216
217
else :
217
218
if not pyfuncitem .session .config .option .mtr_disable_gc :
@@ -227,7 +228,7 @@ def pytest_make_parametrize_id(config, val, argname):
227
228
228
229
229
230
@pytest .hookimpl (hookwrapper = True )
230
- def pytest_sessionstart (session ):
231
+ def pytest_sessionstart (session : pytest . Session ):
231
232
"""
232
233
Instantiate a monitor session to save collected metrics.
233
234
We yield at the end to let pytest pursue the execution.
@@ -251,15 +252,14 @@ def pytest_sessionstart(session):
251
252
session .pytest_monitor = PyTestMonitorSession (
252
253
db = db , remote = remote , component = component , scope = session .config .option .mtr_scope
253
254
)
254
- global PYTEST_MONITORING_ENABLED
255
- PYTEST_MONITORING_ENABLED = not session .config .option .mtr_none
255
+ session .stash [config_stash ] = PyTestMonitorConfig (enabled = not session .config .option .mtr_none )
256
256
session .pytest_monitor .compute_info (session .config .option .mtr_description , session .config .option .mtr_tags )
257
257
yield
258
258
259
259
260
260
@pytest .fixture (autouse = True , scope = "module" )
261
- def _prf_module_tracer (request ):
262
- if not PYTEST_MONITORING_ENABLED :
261
+ def _prf_module_tracer (request : pytest . FixtureRequest ):
262
+ if not request . session . stash [ config_stash ]. enabled :
263
263
yield
264
264
else :
265
265
t_a = time .time ()
@@ -288,14 +288,14 @@ def _prf_module_tracer(request):
288
288
289
289
@pytest .fixture (autouse = True )
290
290
def _prf_tracer (request : pytest .FixtureRequest ):
291
- if not PYTEST_MONITORING_ENABLED :
291
+ if not request . session . stash [ config_stash ]. enabled :
292
292
yield
293
293
else :
294
294
ptimes_a = request .session .pytest_monitor .process .cpu_times ()
295
295
yield
296
296
ptimes_b = request .session .pytest_monitor .process .cpu_times ()
297
297
if not request .node .monitor_skip_test and getattr (request .node , "monitor_results" , False ):
298
- item_name = request .node .originalname or request . node . name
298
+ item_name = request .node .originalname
299
299
item_loc , * _ = request .node .location
300
300
request .session .pytest_monitor .add_test_info (
301
301
item_name ,
0 commit comments