Skip to content

Commit 213f122

Browse files
author
likanglin
committed
condition can be evaluated in the real time
use the skipping.evaluate_condition from pytest in order to keep the same behavior with pytest.mark.skipif. It evaluates the condition in real time so that we can decide whether the case should be rerun in runtime Change-Id: I2575d368c79480223c84498513a4ef605db0c576
1 parent 1471840 commit 213f122

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

README.rst

+14
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ You can also specify an optional ``condition`` in the re-run marker:
108108
import random
109109
assert random.choice([True, False])
110110
111+
You can use ``@pytest.mark.flaky(condition)`` similarly as ``@pytest.mark.skipif(condition)``, see `pytest-mark-skipif <https://docs.pytest.org/en/6.2.x/reference.html#pytest-mark-skipif>`_
112+
113+
.. code-block:: python
114+
115+
@pytest.mark.flaky(reruns=2,condition="sys.platform.startswith('win32')")
116+
def test_example():
117+
import random
118+
assert random.choice([True, False])
119+
# totally same as the above
120+
@pytest.mark.flaky(reruns=2,condition=sys.platform.startswith("win32"))
121+
def test_example():
122+
import random
123+
assert random.choice([True, False])
124+
111125
Note that the test will re-run for any ``condition`` that is truthy.
112126

113127
Output

pytest_rerunfailures.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pkg_resources
66
import pytest
7+
from _pytest import skipping
78
from _pytest.runner import runtestprotocol
89

910
HAS_RESULTLOG = False
@@ -184,7 +185,7 @@ def get_reruns_condition(item):
184185

185186
condition = True
186187
if rerun_marker is not None and "condition" in rerun_marker.kwargs:
187-
condition = rerun_marker.kwargs["condition"]
188+
condition = skipping.evaluate_condition(item, rerun_marker, rerun_marker.kwargs["condition"])[0]
188189

189190
return condition
190191

test_pytest_rerunfailures.py

+9
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,12 @@ def test_fail_two():
550550

551551
result = testdir.runpytest()
552552
assert_outcomes(result, passed=0, failed=1, rerun=expected_reruns)
553+
554+
555+
retryBool = False
556+
@pytest.mark.flaky(condition="retryBool", reruns=2)
557+
def test_str_condition():
558+
global retryBool
559+
retryBool = True
560+
assert False
561+

0 commit comments

Comments
 (0)