pytest-dev / pytest-dev/pytest

Session scoped fixtures can leak out of a class

Open
#2,145 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Best explained with a failing test, see:

https://github.com/jinty/pytest/commit/c0048b115314dd4a54d1fea5032d85f52efd221b

or inline for the lazy:

import pytest

@pytest.fixture(scope='session')
def foo():
    return 'foo'

@pytest.fixture(scope='session')
def bar(foo):
    return foo + 'bar'

@pytest.fixture(scope='session')
def baz(bar):
    return bar + 'baz'

class TestSpecific:

    @pytest.fixture(scope='session')
    def foo(self):
        return 'FOO'

    @pytest.fixture(scope='session')
    def baz(self, bar):
        return bar + 'BAZ'

    def test_b(self, foo, bar, baz):
        pass

def test_c(foo, bar, baz):
    assert foo == 'foo'
    assert bar == 'foobar'
    assert baz == 'foobarbaz'

Fails with the current master.

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 inline Python reproducer, or inspect commit c0048b115314dd4a54d1fea5032d85f52efd221b, to trace how session-scoped fixtures defined inside TestSpecific affect later tests. Done means test_c receives the module-level foo, bar, and baz values and its assertions pass without breaking the class-scoped fixture behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing
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.