The scope of the device does not work when parameterized tests use parameterized lights

I want to divide the instruments between different instances of the same parameterized tests, where the instruments themselves are also parameterized:

#!/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 test_d exchange fixture_2(4) . The problem is that test_c use different versions of fixture_1 .

This also happens when the scopes are defined as โ€œmoduleโ€ and โ€œclassโ€, and this only happens when parameterizing both the test and the instrument.

From the way pytest prints the parameters of testr, it seems that it does not distinguish between the different types of parameters used for each element, so it creates a binding for each set of parameters, and not for each unique subset of parameters the list that the device uses.

Is this a bug in pytest, or have I neglected setting any configuration or something else? Is there a workaround?

+5
source share

Source: https://habr.com/ru/post/1208370/


All Articles