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.
pip install pytest-runtime-xfailVerified import paths — ran on the pinned version, not inferred.
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.
Always provide a string argument to `runtime_xfail()` explaining why the test is being marked as xfail, e.g., `runtime_xfail('Known issue with X on Y platform')`.Place the `runtime_xfail()` call at the beginning of the conditional block, before any code that might cause the test to fail if the condition is met. Ensure `runtime_xfail` is called *before* any potentially failing assertions it is meant to 'cover'.
Prefer `runtime_xfail` for conditions determined only at runtime. For conditions known at collection time (e.g., specific Python version or installed library), `@pytest.mark.xfail` or `@pytest.mark.skipif` are often more appropriate. Understand the interaction to avoid unexpected test outcomes.