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

Test is not marked as xfailed when pytest.xfail() called after a failure. #139

Open
harmin-parra opened this issue Aug 20, 2023 · 2 comments

Comments

@harmin-parra
Copy link

harmin-parra commented Aug 20, 2023

Issue #134 does have a fix.

Sure, it isn't a trivial solution, but it is possible to fix it.

In the pytest_runtest_makereport you need to filter the test outcome report.
Exceptions during execution can be retrieved with call.excinfo._excinfo.

The fix can resemble something like this:

def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()

    num_failures = check_log._num_failures
    failures = check_log.get_failures()
    check_log.clear_failures()

    if report.when == 'call':
        # Don't modify test outcome status for xfailed/skipped tests provoked by pytest.fail or pytest.skip calls
        if hasattr(call, 'excinfo') \
                and call.excinfo is not None \
                and call.excinfo.typename in ('Failed', 'Skipped') \
                and hasattr(call.excinfo.value, "msg")
            return

        # Don't modify test outcome status for xfailed tests provoked by pytest.xfail calls
        if hasattr(call, 'excinfo') \
                and call.excinfo is not None \
                and call.excinfo.typename not in ('Failed', 'Skipped')\
                and hasattr(call.excinfo, '_excinfo') \
                and call.excinfo._excinfo is not None \
                and isinstance(call.excinfo._excinfo, tuple) \
                and len(call.excinfo._excinfo) > 1:
            return

    if failures:
        if item._store[xfailed_key]:
            report.outcome = "skipped"
            report.wasxfail = item._store[xfailed_key].reason
        else:

            summary = f"Failed Checks: {num_failures}"
            longrepr = ["\n".join(failures)]
            longrepr.append("-" * 60)
            longrepr.append(summary)

            if report.longrepr:
                longrepr.append("-" * 60)
                longrepr.append(report.longreprtext)
                report.longrepr = "\n".join(longrepr)
            else:
                report.longrepr = "\n".join(longrepr)
            report.outcome = "failed"
            try:
                raise AssertionError(report.longrepr)
            except AssertionError:
                excinfo = ExceptionInfo.from_current()
            call.excinfo = excinfo
@okken
Copy link
Owner

okken commented Aug 29, 2023

Interesting. Do you want to work on this?

@okken okken changed the title Test is not marked as xfailed Test is not marked as xfailed when pytest.xfail() called after a failure. Aug 29, 2023
@harmin-parra
Copy link
Author

Sure, I can take a look at that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants