Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #20 strict xfail exception #21

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions pytest_retry/retry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,15 @@ def has_interactive_exception(call: pytest.CallInfo) -> bool:
return True


def should_handle_retry(rep: pytest.TestReport) -> bool:
# if test passed, don't retry
if rep.passed:
def should_handle_retry(call: pytest.CallInfo) -> bool:
if call.excinfo is None:
return False
# if teardown stage, don't retry
if rep.when == "teardown":
# may handle fixture setup retries in v2 if requested. For now, this is fine.
if call.when in {"setup", "teardown"}:
return False
# if test was skipped, don't retry
if rep.skipped:
return False
# if test is xfail, don't retry
if hasattr(rep, "wasxfail"):
if call.excinfo.typename == "Skipped":
return False
return True

Expand All @@ -167,7 +164,11 @@ def pytest_runtest_makereport(
retry_manager.record_node_stats(original_report)
# Set dynamic outcome for each stage until runtest protocol has completed.
item.stash[outcome_key] = original_report.outcome
if not should_handle_retry(original_report):

if not should_handle_retry(call):
return
# xfail tests don't raise a Skipped exception if they fail, but are still marked as skipped
if original_report.skipped is True:
return

flake_mark = item.get_closest_marker("flaky")
Expand All @@ -192,8 +193,6 @@ def pytest_runtest_makereport(
hook = item.ihook

while True:
if call.when == "setup":
break # will handle fixture setup retries in v2, if necessary. For now, this is fine.
# Default teardowns are already excluded, so this must be the `call` stage
# Try preliminary teardown using a fake item to ensure every local fixture (i.e.
# excluding session) is torn down. Yes, including module and class fixtures
Expand Down
20 changes: 17 additions & 3 deletions tests/test_retry_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_no_retry_on_pass(testdir):


def test_no_retry_on_fail_without_plugin(testdir):
testdir.makepyfile("def test_success(): assert False")
testdir.makepyfile("def test_failure(): assert False")
result = testdir.runpytest()

assert_outcomes(result, passed=0, failed=1, retried=0)
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_xfail():
)
result = testdir.runpytest("--retries", "1")

assert_outcomes(result, passed=0, xfailed=1)
assert_outcomes(result, passed=0, xfailed=1, failed=0)


def test_no_retry_on_xpass(testdir):
Expand All @@ -97,7 +97,21 @@ def test_xpass():
)
result = testdir.runpytest("--retries", "1")

assert_outcomes(result, passed=0, xpassed=1)
assert_outcomes(result, passed=0, xpassed=1, failed=0)


def test_no_retry_on_strict_xpass(testdir):
testdir.makepyfile(
"""
import pytest
@pytest.mark.xfail(strict=True)
def test_xpass():
assert 1 == 1
"""
)
result = testdir.runpytest("--retries", "1")

assert_outcomes(result, passed=0, xpassed=0, failed=1)


def test_retry_fails_after_consistent_setup_failure(testdir):
Expand Down
Loading