pytest-dev / pytest-dev/pytest

Module Scoped Fixture Invoked Multiple Times with Indirect Parameterized Tests

Open
#12,459 1 comment 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

Description:

When using indirect parameterization with a module-scoped fixture, the fixture is invoked multiple times instead of once per module. This behavior seems incorrect, as it defeats the purpose of having a module-scoped fixture.

Code to Reproduce:

import pytest

@pytest.fixture(scope='module')
def setup_environment():
    print("Setting up environment")
    yield
    print("Tearing down environment")

@pytest.fixture(scope='module')
def config(request):
    print("Setting up config")
    return request.param

@pytest.mark.parametrize('config', ['config1', 'config2'], indirect=True)
def test_example(setup_environment, config):
    print(f"Running test with config: {config}")

Output:

$ pytest -s
Setting up config
Setting up environment
Running test with config: config1
.Tearing down environment
Setting up config
Setting up environment
Running test with config: config2
.Tearing down environment

Expected Behavior:

The setup_environment fixture should be invoked only once per module, not once per test function.

Additional Context:

This issue disrupts test isolation and setup efficiency, especially in cases where the environment setup is costly.

Pip list:

$ pip list
Package                  Version
------------------------ --------
beautifulsoup4           4.12.3
build                    1.2.1
cachetools               5.3.3
certifi                  2024.2.2
cfgv                     3.4.0
charset-normalizer       3.3.2
click                    8.1.7
distlib                  0.3.8
exceptiongroup           1.2.0
filelock                 3.13.1
google                   3.0.0
google-api-core          2.18.0
google-api-python-client 2.123.0
google-auth              2.29.0
google-auth-httplib2     0.2.0
googleapis-common-protos 1.63.0
grpcio                   1.62.1
grpcio-tools             1.62.1
httplib2                 0.22.0
identify                 2.5.33
idna                     3.6
iniconfig                2.0.0
nodeenv                  1.8.0
packaging                23.2
pip                      24.0
pip-tools                7.4.1
platformdirs             4.1.0
pluggy                   1.3.0
pre-commit               3.6.0
proto-plus               1.23.0
protobuf                 4.25.1
pyasn1                   0.5.1
pyasn1-modules           0.3.0
pyparsing                3.1.2
pyproject_hooks          1.1.0
pytest                   7.4.3
PyYAML                   6.0.1
requests                 2.31.0
rsa                      4.9
setuptools               69.2.0
soupsieve                2.5
tomli                    2.0.1
uritemplate              4.1.1
urllib3                  2.2.1
virtualenv               20.25.0
wheel                    0.43.0

OS and version:

Fedora release 40 (Forty)
platform linux -- Python 3.12.3, pytest-7.4.3, pluggy-1.3.0

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 provided pytest reproducer with indirect parameterization and module-scoped fixtures, then trace fixture caching and parametrization handling in pytest. Add a regression test for the shown setup and verify that setup_environment runs once per module while both configuration values are tested.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.