pytest-dev / pytest-dev/pytest

Indirect parametrization with scope="function" does not always work

Open
#11,575 2 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

Indirect parametrization with scope="function" (or not specifying scope) does not seem to work when the following two conditions match:

  1. The previous test's request param scope is larger than function
  2. The current test's parametrized request value is identical to the previous test's
  • Code to reproduce
import pytest


@pytest.fixture(scope="module")
def fixture(request):
    print(f"fixture (scope={request.scope})")


@pytest.mark.parametrize("fixture", ["a", "b"], scope="module", indirect=True)
def test1(fixture):
    ...


@pytest.mark.parametrize("fixture", ["b", "c"], scope="function", indirect=True)
def test2(fixture):
    ...


@pytest.mark.parametrize("fixture", ["c", "d"], scope="function", indirect=True)
def test3(fixture):
    ...
  • Result
$ pytest -s -v
======================================================= test session starts ========================================================
platform darwin -- Python 3.9.18, pytest-7.4.3, pluggy-1.3.0 -- /Users/yugo/.pyenv/versions/3.9.18/envs/test_3.9.18/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/yugo/Desktop/test
collected 6 items                                                                                                                  

test_something.py::test1[a] fixture (scope=module)
PASSED
test_something.py::test1[b] fixture (scope=module)
PASSED
test_something.py::test2[b] PASSED
test_something.py::test2[c] fixture (scope=function)
PASSED
test_something.py::test3[c] fixture (scope=function)
PASSED
test_something.py::test3[d] fixture (scope=function)
PASSED

Note that scope="function" is not working in the test test_something.py::test2[b] that matches the above condition.

  • System environment
$ pip list
Package        Version
-------------- -------
exceptiongroup 1.1.3
iniconfig      2.0.0
packaging      23.2
pip            23.0.1
pluggy         1.3.0
pytest         7.4.3
setuptools     58.1.0
tomli          2.0.1

$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.7
BuildVersion:	19H1217

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 reproducer in test_something.py with pytest -s -v and compare fixture behavior for test1, test2, and test3. Trace pytest's fixture and parametrization handling for repeated values across module and function scopes. Done means test2[b] receives function-scoped behavior and a regression test covers the reported sequence.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.