Skip to content

Scope of parameterized fixture doesn't work #12086

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
gjambaisivanandham opened this issue Mar 7, 2024 · 3 comments
Open

Scope of parameterized fixture doesn't work #12086

gjambaisivanandham opened this issue Mar 7, 2024 · 3 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed

Comments

@gjambaisivanandham
Copy link

gjambaisivanandham commented Mar 7, 2024

I am fairly new to pytest and I see that scope of a parameterized fixture is not working.

Here is an example of the fixture and the test:

@pytest.fixture(scope='session')
def my_fixture(request):
       # make some API calls
       # print API call response
       return response

Tests

@pytest.mark.parametrize('my_fixture',['a','b']):
def test_scenario_1(my_fixture):
      assert response['text'] == 'abc' 

@pytest.mark.parametrize('my_fixture',['a','b']):
def test_scenario_2(my_fixture):
      assert response['image'] == 'def' 

When I run the tests I see the API responses printed 4 times(twice for a parameter and twice for b parameter). I was expecting it to be printed just twice(once for both the parameters - a and b) since both the tests use same set of parameters and the fixed is scoped session. Obviously, if I don't parameterize the fixture the api response is printed once. Pytest version is 7.4.2

@RonnyPfannschmidt
Copy link
Member

I believe there was a fix for this in the 8 series

We should warn better about implicit indirect parameters having different scopes

@gjambaisivanandham
Copy link
Author

I believe there was a fix for this in the 8 series

We should warn better about implicit indirect parameters having different scopes

I just tried with the latest version 8.0.2 and the issue exists in it.

@Zac-HD Zac-HD added type: bug problem that needs to be addressed topic: fixtures anything involving fixtures directly or indirectly labels Mar 12, 2024
@WarrenTheRabbit
Copy link
Contributor

Thank you for posting, @gjambaisivanandham. I'm new to pytest as well so I am interested in exploring and understanding your issue.

These may be silly remarks coming from my ignorance of session scope but:

  • do your examples have syntax errors? It looks to me like response isn't defined in the body of your test functions, for example.
  • aren't you overriding the session fixture by directly parametrizing the test functions?
  • to not override your session fixture, shouldn't indirect=True be used?

Is this the fixture usage your are expecting from file.py below. Note that test_scenario_1 uses indirect=True in my example.

import pytest

@pytest.fixture(scope='session')
def my_fixture(request):
    """I am being used."""
    pass
    
@pytest.mark.parametrize('my_fixture', ['a','b'], indirect=True)
def test_scenario_1(my_fixture):
    pass

@pytest.mark.parametrize('my_fixture', ['a','b'])
def test_scenario_2(my_fixture):
    pass

Fixture usage:

$ pytest file.py --fixtures-per-test
-------------------------- fixtures used by test_scenario_1[a] ---------------------------
-------------------------------------- (file.py:10) --------------------------------------
my_fixture -- file.py:4
    I am being used.

-------------------------- fixtures used by test_scenario_1[b] ---------------------------
-------------------------------------- (file.py:10) --------------------------------------
my_fixture -- file.py:4
    I am being used.

-------------------------- fixtures used by test_scenario_2[a] ---------------------------
-------------------------------------- (file.py:14) --------------------------------------
my_fixture -- src/_pytest/python.py:1113
    no docstring available

-------------------------- fixtures used by test_scenario_2[b] ---------------------------
-------------------------------------- (file.py:14) --------------------------------------
my_fixture -- src/_pytest/python.py:1113
    no docstring available
  • @RonnyPfannschmidt: are you saying the parametrize mark should implicitly do parametrization of the session fixture here rather than creating a pseudo fixture for the argument my_fixture?

Thank you again, @gjambaisivanandham!

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 type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

4 participants