{"id":10151,"library":"pytest-runtime-xfail","title":"Pytest Runtime Xfail","description":"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.","status":"active","version":"1.1.1","language":"en","source_language":"en","source_url":"https://github.com/kalekdan/pytest-runtime-xfail","tags":["pytest","testing","xfail","plugin","conditional"],"install":[{"cmd":"pip install pytest-runtime-xfail","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"This is a pytest plugin and requires pytest to function.","package":"pytest","optional":false}],"imports":[{"note":"`runtime_xfail` is provided as a pytest fixture; it should be requested directly in your test function signature, not imported from the package.","wrong":"from pytest_runtime_xfail import runtime_xfail","symbol":"runtime_xfail","correct":"def test_example(runtime_xfail):"}],"quickstart":{"code":"import platform\n\ndef test_feature_on_linux_only(runtime_xfail):\n    \"\"\"This test expects to pass on Linux, fail on others.\"\"\"\n    if platform.system() != \"Linux\":\n        runtime_xfail(f\"This feature is not supported on {platform.system()}\")\n    \n    # Simulate a test that would fail on non-Linux systems\n    assert platform.system() == \"Linux\", \"Expected to be on Linux\"\n\ndef test_conditional_bug(runtime_xfail):\n    \"\"\"This test demonstrates runtime xfail for a known bug under a condition.\"\"\"\n    # Imagine some condition based on a runtime value or environment\n    is_bug_present = True # This would be dynamically determined\n\n    if is_bug_present:\n        runtime_xfail(\"Known bug #123 causing failure under this condition\")\n    \n    # Simulate a failing assertion if the bug is present\n    assert False, \"This assertion will fail if bug is present and not xfailed\"\n","lang":"python","description":"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."},"warnings":[{"fix":"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')`.","message":"Calling `runtime_xfail()` without a reason string will raise a TypeError.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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'.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Add `runtime_xfail` as an argument to your test function: `def test_my_feature(runtime_xfail):`","cause":"Attempting to use `runtime_xfail` without requesting it as a fixture in the test function signature.","error":"NameError: name 'runtime_xfail' is not defined"},{"fix":"Provide a descriptive string for why the test is being xfailed: `runtime_xfail('Specific condition not met')`","cause":"Calling `runtime_xfail()` without providing a reason string.","error":"TypeError: runtime_xfail() missing 1 required positional argument: 'reason'"},{"fix":"Ensure `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.","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.","error":"Test still fails instead of being marked as XFAIL"}]}