Fixture variants with different parameters currently cannot coexist #8350
-
Hi there, I am wondering, in case you have some expensive setups, if it would be possible to avoid repeating the fixture execution. import pytest
@pytest.fixture(scope="session")
def param1(request):
print("session param1 is {}".format(request.param))
@pytest.fixture(scope="session")
def param2(request):
print("session param2 is {}".format(request.param))
@pytest.mark.parametrize('param1', [1, 2], indirect=True)
@pytest.mark.parametrize('param2', [1, 2], indirect=True)
def test_example(param1, param2):
print("Test done\n") Provides the following output. $ pytest -s
========================= test session starts =========================
platform linux -- Python 3.8.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/.../Temp/test
collected 4 items
test_example.py session param1 is 1
session param2 is 1 # <-- FIRST execution of param2 with 1
Test done
.session param2 is 2
Test done
.session param1 is 2
Test done
.session param2 is 1 # <-- SECOND execution of param2 with 1
Test done
.
========================== 4 passed in 0.01s ========================== As I defined the fixture as "session" I do not expect to have the fixture running a second time with the same parameter. How do you think it would be the best way to implement this? Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
xdist has examples on how to sync fixtures between local processes as for having multiple versions coexist, a common pattern is to have a manager type fixture that deals teaedown and having utility fixture that use the manager for setup and teardown, so that setup happens as needed and teardown after usage the utility fixtures would still be recreated multiple times, but the work would cache in the manager fixture |
Beta Was this translation helpful? Give feedback.
-
I see, something like this probably? import pytest
@pytest.fixture(scope="session")
def manager(request):
print("session param1 is {}".format(request.param))
@pytest.fixture(scope="function")
def util(request, manager):
print("session param2 is {}".format(request.param))
@pytest.mark.parametrize('manager', [1, 2], indirect=True)
@pytest.mark.parametrize('util', [1, 2], indirect=True)
def test_example(util):
print("Test done\n") That would print: $ pytest -s
========================= test session starts =========================
platform linux -- Python 3.8.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /home/.../Temp/test
collected 4 items
test_example.py session param1 is 1
session param2 is 1
Test done
.session param2 is 2
Test done
.session param1 is 2
session param2 is 1
Test done
.session param2 is 2
Test done
.
========================== 4 passed in 0.01s ========================== Not exactly what I was looking for, as it runs more than 4 tests, but I undestand the idea behind. |
Beta Was this translation helpful? Give feedback.
-
its not clear what you are asking for, can you make up a example output? |
Beta Was this translation helpful? Give feedback.
xdist has examples on how to sync fixtures between local processes
as for having multiple versions coexist, a common pattern is to have a manager type fixture that deals teaedown and having utility fixture that use the manager for setup and teardown, so that setup happens as needed and teardown after usage
the utility fixtures would still be recreated multiple times, but the work would cache in the manager fixture