6
6
import memory_profiler
7
7
import pytest
8
8
9
- from pytest_monitor .models import PyTestMonitorConfig
9
+ from pytest_monitor .markers import PyTestMonitorMarkerProcessor
10
+ from pytest_monitor .models import PyTestMonitorConfig , PyTestMonitorItemConfig
10
11
from pytest_monitor .session import PyTestMonitorSession
11
12
12
- # These dictionaries are used to compute members set on each items.
13
- # KEY is the marker set on a test function
14
- # value is a tuple:
15
- # expect_args: boolean
16
- # internal marker attribute name: str
17
- # callable that set member's value
18
- # default value
19
- PYTEST_MONITOR_VALID_MARKERS = {
20
- "monitor_skip_test" : (False , "monitor_skip_test" , lambda x : True , False ),
21
- "monitor_skip_test_if" : (True , "monitor_skip_test" , lambda x : bool (x ), False ),
22
- "monitor_test" : (False , "monitor_force_test" , lambda x : True , False ),
23
- "monitor_test_if" : (True , "monitor_force_test" , lambda x : bool (x ), False ),
24
- }
25
- PYTEST_MONITOR_DEPRECATED_MARKERS = {}
26
-
27
13
config_stash = pytest .StashKey [PyTestMonitorConfig ]()
14
+ item_config_stash = pytest .StashKey [PyTestMonitorItemConfig ]()
15
+
16
+ marker_processor = PyTestMonitorMarkerProcessor ()
28
17
29
18
30
19
def pytest_addoption (parser ):
@@ -102,7 +91,7 @@ def pytest_configure(config):
102
91
config .addinivalue_line ("markers" , "monitor_skip_test: mark test to be executed but not monitored." )
103
92
config .addinivalue_line (
104
93
"markers" ,
105
- "monitor_skip_test_if(cond): mark test to be executed but " " not monitored if cond is verified." ,
94
+ "monitor_skip_test_if(cond): mark test to be executed but not monitored if cond is verified." ,
106
95
)
107
96
config .addinivalue_line (
108
97
"markers" ,
@@ -126,36 +115,8 @@ def pytest_runtest_setup(item: pytest.Item):
126
115
"""
127
116
if not item .session .stash [config_stash ].enabled :
128
117
return
129
- item_markers = {mark .name : mark for mark in item .iter_markers () if mark and mark .name .startswith ("monitor_" )}
130
- mark_to_del = []
131
- for set_marker in item_markers .keys ():
132
- if set_marker not in PYTEST_MONITOR_VALID_MARKERS :
133
- warnings .warn ("Nothing known about marker {}. Marker will be dropped." .format (set_marker ))
134
- mark_to_del .append (set_marker )
135
- if set_marker in PYTEST_MONITOR_DEPRECATED_MARKERS :
136
- warnings .warn (f"Marker { set_marker } is deprecated. Consider upgrading your tests" )
137
-
138
- for marker in mark_to_del :
139
- del item_markers [marker ]
140
-
141
- all_valid_markers = PYTEST_MONITOR_VALID_MARKERS
142
- all_valid_markers .update (PYTEST_MONITOR_DEPRECATED_MARKERS )
143
- # Setting instantiated markers
144
- for marker , _ in item_markers .items ():
145
- with_args , attr , fun_val , _ = all_valid_markers [marker ]
146
- attr_val = fun_val (item_markers [marker ].args [0 ]) if with_args else fun_val (None )
147
- setattr (item , attr , attr_val )
148
-
149
- # Setting other markers to default values
150
- for marker , marker_value in all_valid_markers .items ():
151
- with_args , attr , _ , default = marker_value
152
- if not hasattr (item , attr ):
153
- setattr (item , attr , default )
154
118
155
- # Finalize marker processing by enforcing some marker's value
156
- if item .monitor_force_test :
157
- # This test has been explicitly flagged as 'to be monitored'.
158
- item .monitor_skip_test = False
119
+ item .stash [item_config_stash ] = marker_processor (item )
159
120
160
121
161
122
@pytest .hookimpl (tryfirst = True , hookwrapper = True )
@@ -294,7 +255,7 @@ def _prf_tracer(request: pytest.FixtureRequest):
294
255
ptimes_a = request .session .pytest_monitor .process .cpu_times ()
295
256
yield
296
257
ptimes_b = request .session .pytest_monitor .process .cpu_times ()
297
- if not request .node .monitor_skip_test and getattr (request .node , "monitor_results" , False ):
258
+ if not request .node .stash [ item_config_stash ]. skip and getattr (request .node , "monitor_results" , False ):
298
259
item_name = request .node .originalname
299
260
item_loc , * _ = request .node .location
300
261
request .session .pytest_monitor .add_test_info (
0 commit comments