pytest-dev / pytest-dev/pytest

Error not raised in session fixture when mark.xfail is set on last test

Open
#8,375 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Version
6.2.2

Context
Running a test suite with a session fixture that does some cleaning up

Description
If the last test of the suite has a mark.xfail, the error will be marked as an additional xfailed test

Minimal example

import pytest

@pytest.fixture(autouse=True, scope='session')
def failme():
    yield

    raise RuntimeError('cleanup fails for some reason')

def test_01_OK():
    assert 1 == 1

@pytest.mark.xfail()
def test_02_fail():
    assert 0 == 1
% py.test -v test.py
test.py::test_01_OK PASSED
test.py::test_02_fail XFAIL
test.py::test_02_fail XFAIL

To me, it would seem more appropriate to raise the error since the fixture is executed in the context of the session, not the test

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 running the minimal example in test.py with pytest -v and observe the session-fixture cleanup failure after the final xfailed test. Trace how session fixture teardown errors are reported in this execution path. Done means the cleanup RuntimeError is raised or reported as an error rather than recorded as an additional xfailed test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.