pytest-dev / pytest-dev/pytest

Scoped fixtures are not rebuilt when the fixtures they depend on change

Open
#14,095 4 comments 0 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

When module-scoped fixture b depends on module-scoped fixture a, I was expecting fixture b to be rebuilt if the value/source of a was changed. In code, I expect all of the following assertions to succeed:

from pytest import fixture

@fixture(scope="module")
def a():
    return 0

@fixture(scope="module")
def b(a):
    return a

def test_b_is_zero(b):
    assert b == 0

class TestClass:
    @fixture(scope="module")
    def a(self):
        return 1

    def test_b_is_one(self, b):
        assert b == 1

However, the fixture that the test within the class is presented with is not as expected:

============================================ test session starts =============================================
platform linux -- Python 3.14.0, pytest-9.0.2, pluggy-1.6.0 -- /home/rob/tmp/fixtures/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/rob/tmp/fixtures
configfile: pyproject.toml
collected 2 items                                                                                            

test_a.py::test_b_is_zero PASSED                                                                       [ 50%]
test_a.py::TestClass::test_b_is_one FAILED                                                             [100%]

================================================== FAILURES ==================================================
__________________________________________ TestClass.test_b_is_one ___________________________________________

self = <test_a.TestClass object at 0x7f235325fb10>, b = 0

    def test_b_is_one(self, b):
>       assert b == 1
E       assert 0 == 1

test_a.py:24: AssertionError
========================================== short test summary info ===========================================
FAILED test_a.py::TestClass::test_b_is_one - assert 0 == 1
======================================== 1 failed, 1 passed in 0.03s =========================================
% uv pip list
Package   Version
--------- -------
iniconfig 2.3.0
packaging 25.0
pluggy    1.6.0
pygments  2.19.2
pytest    9.0.2

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 saving the reproducer from the issue as test_a.py and run it with pytest. Trace how module-scoped fixtures a and b are resolved and reused across the module and TestClass scopes. Done means both test_b_is_zero and TestClass.test_b_is_one pass, with b rebuilt from the class-scoped a.

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
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.