-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Wrong session scoped fixtures parametrization #2844
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
Comments
Thanks for the report. This is really strange. I managed to reduce the example further: import pytest
@pytest.fixture(scope="session")
def session_wrapper(param1):
return param1
class TestClass:
@pytest.fixture(scope="session")
def session_wrapper(self, session_wrapper):
return session_wrapper
@pytest.fixture(scope="session",
params=["inside A", "inside B"])
def param1(self, request):
return request.param
def test_inside(self, session_wrapper, param1):
assert session_wrapper == param1
@pytest.fixture(scope="session",
params=["outside A", "outside B"])
def param1(request):
return request.param
def test_outside(session_wrapper, param1):
assert session_wrapper == param1 And here is the full execution and failure:
|
The same issue happens across files too.
Execution and failure:
|
The problem occurs for I put @pytest.fixture(scope="session")
def session_wrapper(param1):
import pdb; pdb.set_trace()
return param1 and it drops into the debugger every time BUT NOT this one after scope switching (from class test <-> module test):
|
Discovered this today... I think it's a variation on this ticket -- no parameterization needed? It seems like just instantiating the session-scope fixture once (not surprising?), but when it does, it uses the overrides available at the time of first use. It could be surprising that IMO, the fix for this is either:
# conftest.py
@pytest.fixture(scope='session')
def session_wrapper(param):
print("\nroot: session_wrapper: start")
yield param
print("\nroot: session_wrapper: done")
@pytest.fixture(scope='session')
def param():
print("\nroot: param: start") # always printed if test_file_2 is run
yield 'root'
print("\nroot: param: done") # always printed if test_file_2 is run # test_1.py
@pytest.fixture(scope='session')
def param():
print("\nfile 1: param: start") # always printed if test_file_1 is run
yield 'file 1'
print("\nfile 1: param: done") # always printed if test_file_1 is run
def test_file_1(session_wrapper):
print(session_wrapper) # prints either 'file 1' or 'root', depending on which test ran first # test_2.py
def test_file_2(session_wrapper):
print(session_wrapper) # prints either 'file 1' or 'root', depending on which test ran first Run all tests: both get the test_1.py value. Note that both of the
Run just test_1.py: seems normal (test gets local override, 'file 1').
Run just test_2.py: The test gets the 'root' fixture's value. Seems normal.
|
Consider following testing code:
Session-scoped fixture
session_wrapper
is parametrized byparam1
andparam2
fixtures, which the fixture returns as tuple. Tests check ifsession_wrapper
really returns currentparam1
andparam2
instances. Which is not the case - firstsession_wrapper
instance in new context returns wrong params (from previous context):If
TestClass
is at the end of the script (firstoutside
test and context, theninside
), equivalent result is achieved:When
session_wrapper
hasclass
orfunction
scope, everything works as expected.The text was updated successfully, but these errors were encountered: