Skip to content

Please make it possible to "mark" subtests as xfail #14101

@mgorny

Description

@mgorny

What's the problem this feature will solve?

Currently, it isn't possible to conveniently mark a subtest as XFAIL-ing with full support for xfail_strict=True.

Describe the solution you'd like

Ideally, I'd like to be able to do something like:

def test_foo(subtests):
    with subtests.test(msg=..., xfail=condition):
        ...

that would mark this one subtest as XFAIL, the same way pytest.mark.xfail marks the whole test as XFAIL-ing.

Alternative Solutions

  1. Marking the whole test with pytest.mark.xfail causes it to unconditionally fail with xfail_strict=True. Reading the comments in the code, I infer that it isn't supported. And even if it were, more precision would be desirable.
    $ pytest test.py -v -o xfail_strict=True
    ========================================================= test session starts =========================================================
    platform linux -- Python 3.12.12, pytest-9.1.0.dev148+g0e9db5fa5, pluggy-1.6.0 -- /tmp/foo/.venv/bin/python3
    cachedir: .pytest_cache
    rootdir: /tmp/foo
    collected 1 item                                                                                                                      
    
    test.py::test_foo SUBXFAIL[xfail] (foo)                                                                                         [100%]
    test.py::test_foo FAILED                                                                                                        [100%]
    
    ============================================================== FAILURES ===============================================================
    ______________________________________________________________ test_foo _______________________________________________________________
    [XPASS(strict)] foo
    ======================================================= short test summary info =======================================================
    FAILED test.py::test_foo - [XPASS(strict)] foo
    ==================================================== 1 failed, 1 xfailed in 0.01s =====================================================
  2. It is possible to call pytest.xfail() directly from the test. However, this requires a bit of handiwork to "reimplement" xfail_strict.

Additional context

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions