Pytest Runtime Xfail

1.1.1 · active · verified Fri Apr 17

pytest-runtime-xfail is a pytest plugin that provides a `runtime_xfail` fixture, enabling tests to be marked as 'xfail' (expected to fail) dynamically during test execution. This is particularly useful for tests that might fail under specific, conditional circumstances (e.g., different operating systems, library versions, or data states) that cannot be determined at test collection time. The current version is 1.1.1, and its release cadence is generally low, with updates as needed for bug fixes or compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `runtime_xfail` fixture within a test function. The test requests `runtime_xfail` as an argument, and then calls it conditionally. If `runtime_xfail` is called, the test will be marked as 'XFAIL' (expected to fail) with the provided reason, even if subsequent assertions fail.

import platform

def test_feature_on_linux_only(runtime_xfail):
    """This test expects to pass on Linux, fail on others."""
    if platform.system() != "Linux":
        runtime_xfail(f"This feature is not supported on {platform.system()}")
    
    # Simulate a test that would fail on non-Linux systems
    assert platform.system() == "Linux", "Expected to be on Linux"

def test_conditional_bug(runtime_xfail):
    """This test demonstrates runtime xfail for a known bug under a condition."""
    # Imagine some condition based on a runtime value or environment
    is_bug_present = True # This would be dynamically determined

    if is_bug_present:
        runtime_xfail("Known bug #123 causing failure under this condition")
    
    # Simulate a failing assertion if the bug is present
    assert False, "This assertion will fail if bug is present and not xfailed"

view raw JSON →