Pytest Runtime Xfail
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
-
NameError: name 'runtime_xfail' is not defined
cause Attempting to use `runtime_xfail` without requesting it as a fixture in the test function signature.fixAdd `runtime_xfail` as an argument to your test function: `def test_my_feature(runtime_xfail):` -
TypeError: runtime_xfail() missing 1 required positional argument: 'reason'
cause Calling `runtime_xfail()` without providing a reason string.fixProvide a descriptive string for why the test is being xfailed: `runtime_xfail('Specific condition not met')` -
Test still fails instead of being marked as XFAIL
cause The `runtime_xfail()` call was placed after an assertion that failed, or the condition for calling `runtime_xfail` was not met when an assertion failed.fixEnsure `runtime_xfail()` is called *before* any assertion that it is intended to mark as xfail. Verify your conditional logic correctly triggers `runtime_xfail` when the test is expected to fail.
Warnings
- gotcha Calling `runtime_xfail()` without a reason string will raise a TypeError.
- gotcha If an assertion fails *before* `runtime_xfail()` is called within a test, the test will be marked as FAILED, not XFAIL. `runtime_xfail` needs to be called proactively.
- gotcha Mixing `pytest-runtime-xfail` with the static `@pytest.mark.xfail` can be confusing. While `runtime_xfail` takes precedence if called, using `@pytest.mark.xfail(run=False)` will prevent the test body (and thus `runtime_xfail`) from executing at all.
Install
-
pip install pytest-runtime-xfail
Imports
- runtime_xfail
from pytest_runtime_xfail import runtime_xfail
def test_example(runtime_xfail):
Quickstart
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"