66import memory_profiler
77import pytest
88
9+ from pytest_monitor .models import PyTestMonitorConfig
910from pytest_monitor .session import PyTestMonitorSession
1011
1112# These dictionaries are used to compute members set on each items.
2324}
2425PYTEST_MONITOR_DEPRECATED_MARKERS = {}
2526
26- PYTEST_MONITORING_ENABLED = True
27+ config_stash = pytest . StashKey [ PyTestMonitorConfig ]()
2728
2829
2930def pytest_addoption (parser ):
@@ -117,13 +118,13 @@ def pytest_configure(config):
117118 )
118119
119120
120- def pytest_runtest_setup (item ):
121+ def pytest_runtest_setup (item : pytest . Item ):
121122 """
122123 Validate marker setup and print warnings if usage of deprecated marker is identified.
123124 Setting marker attribute to the discovered item is done after the above described verification.
124125 :param item: Test item
125126 """
126- if not PYTEST_MONITORING_ENABLED :
127+ if not item . session . stash [ config_stash ]. enabled :
127128 return
128129 item_markers = {mark .name : mark for mark in item .iter_markers () if mark and mark .name .startswith ("monitor_" )}
129130 mark_to_del = []
@@ -158,7 +159,7 @@ def pytest_runtest_setup(item):
158159
159160
160161@pytest .hookimpl (tryfirst = True , hookwrapper = True )
161- def pytest_runtest_makereport (item , call ):
162+ def pytest_runtest_makereport (item : pytest . Item , call : pytest . CallInfo ):
162163 """
163164 Used to identify the current call to add times.
164165 :param item: Test item
@@ -172,8 +173,8 @@ def pytest_runtest_makereport(item, call):
172173 setattr (item , "test_effective_start_time" , call .start )
173174
174175
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 :
177178 return
178179 setattr (item , "monitor_results" , False )
179180 if hasattr (item , "module" ):
@@ -187,7 +188,7 @@ def pytest_runtest_call(item):
187188
188189
189190@pytest .hookimpl
190- def pytest_pyfunc_call (pyfuncitem ):
191+ def pytest_pyfunc_call (pyfuncitem : pytest . Function ):
191192 """
192193 Core sniffer logic. We encapsulate the test function in a sniffer function to collect
193194 memory results.
@@ -211,7 +212,7 @@ def prof():
211212 setattr (pyfuncitem , "mem_usage" , memuse )
212213 setattr (pyfuncitem , "monitor_results" , True )
213214
214- if not PYTEST_MONITORING_ENABLED :
215+ if not pyfuncitem . session . stash [ config_stash ]. enabled :
215216 wrapped_function ()
216217 else :
217218 if not pyfuncitem .session .config .option .mtr_disable_gc :
@@ -227,7 +228,7 @@ def pytest_make_parametrize_id(config, val, argname):
227228
228229
229230@pytest .hookimpl (hookwrapper = True )
230- def pytest_sessionstart (session ):
231+ def pytest_sessionstart (session : pytest . Session ):
231232 """
232233 Instantiate a monitor session to save collected metrics.
233234 We yield at the end to let pytest pursue the execution.
@@ -251,15 +252,14 @@ def pytest_sessionstart(session):
251252 session .pytest_monitor = PyTestMonitorSession (
252253 db = db , remote = remote , component = component , scope = session .config .option .mtr_scope
253254 )
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 )
256256 session .pytest_monitor .compute_info (session .config .option .mtr_description , session .config .option .mtr_tags )
257257 yield
258258
259259
260260@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 :
263263 yield
264264 else :
265265 t_a = time .time ()
@@ -288,14 +288,14 @@ def _prf_module_tracer(request):
288288
289289@pytest .fixture (autouse = True )
290290def _prf_tracer (request : pytest .FixtureRequest ):
291- if not PYTEST_MONITORING_ENABLED :
291+ if not request . session . stash [ config_stash ]. enabled :
292292 yield
293293 else :
294294 ptimes_a = request .session .pytest_monitor .process .cpu_times ()
295295 yield
296296 ptimes_b = request .session .pytest_monitor .process .cpu_times ()
297297 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
299299 item_loc , * _ = request .node .location
300300 request .session .pytest_monitor .add_test_info (
301301 item_name ,
0 commit comments