I want to share fixtures between different instantiations of the same parametrized tests, where the fixtures themselves are also parametrized:
#!/usr/bin/py.test -sv
import pytest
numbers_for_fixture = [0]
def pytest_generate_tests(metafunc):
if "config_field" in metafunc.fixturenames:
metafunc.parametrize("config_field", [1], scope='session')
@pytest.fixture(scope = 'session')
def fixture_1(config_field):
numbers_for_fixture[0] += 1
return '\tfixture_1(%s)' % numbers_for_fixture[0]
@pytest.fixture(scope = 'session')
def fixture_2():
numbers_for_fixture[0] += 1
return '\tfixture_2(%s)' % numbers_for_fixture[0]
def test_a(fixture_1):
print('\ttest_a:', fixture_1)
def test_b(fixture_1):
print('\ttest_b:', fixture_1)
@pytest.mark.parametrize('i', range(3))
def test_c(fixture_1, i):
print('\ttest_c[%s]:' % i, fixture_1)
@pytest.mark.parametrize('i', range(3))
def test_d(fixture_2, i):
print('\ttest_d[%s]:' % i, fixture_2)
I get this output:
platform linux -- Python 3.4.1 -- py-1.4.26 -- pytest-2.6.4 -- /usr/bin/python
collecting ... collected 8 items
test.py::test_a[1] test_a: fixture_1(1)
PASSED
test.py::test_b[1] test_b: fixture_1(1)
PASSED
test.py::test_c[1-0] test_c[0]: fixture_1(1)
PASSED
test.py::test_c[1-1] test_c[1]: fixture_1(2)
PASSED
test.py::test_c[1-2] test_c[2]: fixture_1(3)
PASSED
test.py::test_d[0] test_d[0]: fixture_2(4)
PASSED
test.py::test_d[1] test_d[1]: fixture_2(4)
PASSED
test.py::test_d[2] test_d[2]: fixture_2(4)
PASSED
test_a
, test_b
and test_c[0]
all share fixture_1(1)
. All the test_d
s share fixture_2(4)
. The problem is that the test_c
s use different versions of fixture_1
.
This also happens when scopes are set to "module" and "class", and it only happens when both the test and the fixture are parametrized.
From the way pytest prints the test parametesr, it seems like it doesn't distinguish between the different types of parameters used for each item, so it creates a fixture for each set of parameters rather then for each unique subset of a parameters list that the fixture uses.
Is this a bug in pytest, or did I neglect to set some configuration or something? Is there a workaround?