Skip to content

Commit 807095f

Browse files
dheepan2Dheepan R
andauthored
added rerun_except option (#180)
* added option --rerun-except * updated README with rerun_except option * added test for rerun_except Co-authored-by: Dheepan R <dheepan@dheepanMac.local>
1 parent 409822b commit 807095f

4 files changed

Lines changed: 78 additions & 4 deletions

File tree

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ Bug fixes
1010
- Fix crash when pytest-xdist is installed but disabled.
1111
(Thanks to `@mgorny <https://github.com/mgorny>`_ for the PR.)
1212

13+
Features
14+
++++++++
15+
16+
- Added option `--rerun-except` to rerun failed tests those are other than the mentioned Error.
17+
1318

1419
10.2 (2021-09-17)
1520
-----------------

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ would only rerun those errors that match ``AssertionError`` or ``ValueError``:
8989
9090
$ pytest --reruns 5 --only-rerun AssertionError --only-rerun ValueError
9191
92+
Re-run all failures other than matching certain expressions
93+
-----------------------------------------------------------
94+
95+
To re-run only those failures that do not match a certain list of expressions, use the
96+
``--rerun-except`` flag and pass it a regular expression. For example,
97+
the following would only rerun errors other than that match ``AssertionError``:
98+
99+
.. code-block:: bash
100+
101+
$ pytest --reruns 5 --rerun-except AssertionError
102+
103+
Passing the flag multiple times accumulates the arguments, so the following
104+
would only rerun those errors that does not match with ``AssertionError`` or ``OSError``:
105+
106+
.. code-block:: bash
107+
108+
$ pytest --reruns 5 --rerun-except AssertionError --rerun-except OSError
109+
92110
Re-run individual failures
93111
--------------------------
94112

pytest_rerunfailures.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ def pytest_addoption(parser):
8787
default=0,
8888
help="add time (seconds) delay between reruns.",
8989
)
90+
group._addoption(
91+
"--rerun-except",
92+
action="append",
93+
dest="rerun_except",
94+
type=str,
95+
default=None,
96+
help="If passed, only rerun errors other than matching the "
97+
"regex provided. Pass this flag multiple times to accumulate a list "
98+
"of regexes to match",
99+
)
90100

91101

92102
def _get_resultlog(config):
@@ -280,12 +290,23 @@ def _should_hard_fail_on_error(session_config, report):
280290
return False
281291

282292
rerun_errors = session_config.option.only_rerun
283-
if not rerun_errors:
293+
rerun_except_errors = session_config.option.rerun_except
294+
295+
if not rerun_errors and not rerun_except_errors:
296+
284297
return False
285298

286-
for rerun_regex in rerun_errors:
287-
if re.search(rerun_regex, report.longrepr.reprcrash.message):
288-
return False
299+
if rerun_errors:
300+
for rerun_regex in rerun_errors:
301+
if re.search(rerun_regex, report.longrepr.reprcrash.message):
302+
303+
return False
304+
305+
if rerun_except_errors:
306+
for rerun_regex in rerun_except_errors:
307+
if not re.search(rerun_regex, report.longrepr.reprcrash.message):
308+
309+
return False
289310

290311
return True
291312

test_pytest_rerunfailures.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,36 @@ def test_only_rerun_flag(testdir, only_rerun_texts, should_rerun):
552552
)
553553

554554

555+
@pytest.mark.parametrize(
556+
"rerun_except_texts, should_rerun",
557+
[
558+
(["AssertionError"], True),
559+
(["Assertion*"], True),
560+
(["Assertion"], True),
561+
(["ValueError"], False),
562+
(["AssertionError: "], True),
563+
(["ERR"], False),
564+
(["AssertionError", "OSError"], True),
565+
],
566+
)
567+
def test_rerun_except_flag(testdir, rerun_except_texts, should_rerun):
568+
testdir.makepyfile('def test_rerun_except(): raise ValueError("ERR")')
569+
570+
num_failed = 1
571+
num_passed = 0
572+
num_reruns = 1
573+
num_reruns_actual = num_reruns if should_rerun else 0
574+
575+
pytest_args = ["--reruns", str(num_reruns)]
576+
for rerun_except_text in rerun_except_texts:
577+
print(rerun_except_text)
578+
pytest_args.extend(["--rerun-except", rerun_except_text])
579+
result = testdir.runpytest(*pytest_args)
580+
assert_outcomes(
581+
result, passed=num_passed, failed=num_failed, rerun=num_reruns_actual
582+
)
583+
584+
555585
@pytest.mark.parametrize(
556586
"condition, expected_reruns",
557587
[

0 commit comments

Comments
 (0)