This fixture returns a function that, when called inside the test, XFAILs the currently running test. You should prefer using `@pytest.mark.xfail` when possible. However, when a failure condition is too complicated to test statically (e.g. when it depends on the intersection of
(request: pytest.FixtureRequest)
| 38 | |
| 39 | @pytest.fixture |
| 40 | def xfail(request: pytest.FixtureRequest): |
| 41 | """ |
| 42 | This fixture returns a function that, when called inside the test, |
| 43 | XFAILs the currently running test. |
| 44 | |
| 45 | You should prefer using `@pytest.mark.xfail` when possible. However, when a failure |
| 46 | condition is too complicated to test statically (e.g. when it depends on the |
| 47 | intersection of multiple pytest parameters), this is preferable to just calling |
| 48 | `pytest.xfail`. The difference is that `pytest.xfail` will immediately end the test, |
| 49 | while this fixture allows the test to execute, so that it may result in a XPASS. |
| 50 | |
| 51 | xref https://github.com/pandas-dev/pandas/issues/38902 |
| 52 | |
| 53 | Usage |
| 54 | ----- |
| 55 | :: |
| 56 | @pytest.mark.parametrize("param1", [1, 2, 3]) |
| 57 | @pytest.mark.parametrize("param2", [4, 5, 6]) |
| 58 | def test1(xfail): |
| 59 | if param1 == 2 and param2 == 6: |
| 60 | xfail("This combination of parameters is known to fail") |
| 61 | # Test continues, including in the XFAIL'ed use case |
| 62 | |
| 63 | Parameters |
| 64 | ---------- |
| 65 | reason : str |
| 66 | Reason for the expected failure. |
| 67 | strict: bool, optional |
| 68 | If True, the test will be marked as failed if it passes (XPASS). |
| 69 | If False, the test will be marked as passed both if it fails (XFAIL) |
| 70 | or passes (XPASS). |
| 71 | Default: ``xfail_strict`` value in ``pyproject.toml``, or False if absent. |
| 72 | """ |
| 73 | |
| 74 | def _(reason: str, strict: bool | None = None) -> None: |
| 75 | if strict is not None: |
| 76 | marker = pytest.mark.xfail(reason=reason, strict=strict) |
| 77 | else: |
| 78 | marker = pytest.mark.xfail(reason=reason) |
| 79 | request.node.add_marker(marker) |
| 80 | |
| 81 | return _ |
no outgoing calls
no test coverage detected
searching dependent graphs…