pytest-dev / pytest-dev/pytest

Please make it possible to "mark" subtests as `xfail`

Open
#14,101 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how subtests.test, pytest.mark.xfail, and pytest.xfail interact with xfail_strict=True. Compare the existing subtest reporting and strict-XFAIL behavior, then verify that the proposed per-subtest xfail condition reports expected failures and strict unexpected passes without affecting other subtests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.