@@ -175,6 +175,16 @@ def get_reruns_delay(item):
175175 return delay
176176
177177
178+ def get_reruns_condition (item ):
179+ rerun_marker = _get_marker (item )
180+
181+ condition = True
182+ if rerun_marker is not None and "condition" in rerun_marker .kwargs :
183+ condition = rerun_marker .kwargs ["condition" ]
184+
185+ return condition
186+
187+
178188def _remove_cached_results_from_failed_fixtures (item ):
179189 """
180190 Note: remove all cached_result attribute from every fixture
@@ -221,6 +231,19 @@ def _should_hard_fail_on_error(session_config, report):
221231 return True
222232
223233
234+ def _should_not_rerun (item , report , reruns ):
235+ xfail = hasattr (report , "wasxfail" )
236+ is_terminal_error = _should_hard_fail_on_error (item .session .config , report )
237+ condition = get_reruns_condition (item )
238+ return (
239+ item .execution_count > reruns
240+ or not report .failed
241+ or xfail
242+ or is_terminal_error
243+ or not condition
244+ )
245+
246+
224247def pytest_runtest_protocol (item , nextitem ):
225248 """
226249 Note: when teardown fails, two reports are generated for the case, one for
@@ -247,15 +270,8 @@ def pytest_runtest_protocol(item, nextitem):
247270 reports = runtestprotocol (item , nextitem = nextitem , log = False )
248271
249272 for report in reports : # 3 reports: setup, call, teardown
250- is_terminal_error = _should_hard_fail_on_error (item .session .config , report )
251273 report .rerun = item .execution_count - 1
252- xfail = hasattr (report , "wasxfail" )
253- if (
254- item .execution_count > reruns
255- or not report .failed
256- or xfail
257- or is_terminal_error
258- ):
274+ if _should_not_rerun (item , report , reruns ):
259275 # last run or no failure detected, log normally
260276 item .ihook .pytest_runtest_logreport (report = report )
261277 else :
0 commit comments