Skip to content

Module Scoped Fixture Invoked Multiple Times with Indirect Parameterized Tests #12459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pau-alef opened this issue Jun 13, 2024 · 1 comment
Labels
topic: fixtures anything involving fixtures directly or indirectly

Comments

@pau-alef
Copy link

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
@Zac-HD Zac-HD added the topic: fixtures anything involving fixtures directly or indirectly label Jun 24, 2024
@mon
Copy link

mon commented Dec 23, 2024

I'm on pytest 8.3.4 and this appears to be fixed?

tests.py Setting up environment
Setting up config
Running test with config: config1
.Setting up config
Running test with config: config2
.Tearing down environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly
Projects
None yet
Development

No branches or pull requests

3 participants