pytest-dev / pytest-dev/pytest

`pytest.xfail` and `pytest.skip` as context managers

Open
#13,001 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

type: proposal
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?

For some reason we may expect a specific part of the test to fail or to be skipped (maybe conditionally), usually because there's a blocking issue to be resolved.

Describe the solution you'd like

We propose exposing pytest.xfail and pytest.skip (or their equivalents) as context managers, just like pytest.raises. So we may have:

def test_match():
    assert match("hello", "hello")
    with pytest.xfail("external/matcher#123: match doesn't support case-insensitive matching yet"):
        assert match("World", "WORLD")

pytest.skip is used for a similar purpose, only when the broken tests are not even going to be run.

Alternative Solutions

We can, of course, break a single test into multiple and mark them with @pytest.mark.xfail and @pytest.mark.skip. This is less favorable because tests may be organized and can share some setup codes (which may not be exposed by fixture). Most importantly, we may want the test to early fail before it ever touches the with pytest.xfail block, and this is not achievable without the help of plugins like pytest-dependency.

Additional context

We would also like to see strict= argument in the current pytest.xfail.

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 reading the existing pytest.xfail and pytest.skip APIs and compare their behavior with pytest.raises as a context manager. Clarify how scoped skipping and expected failures should behave, including the requested strict= argument; done means the feature is specified and tested for conditional blocks and early failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.