pytest-lazy-fixture icon indicating copy to clipboard operation
pytest-lazy-fixture copied to clipboard

Nested fixtures are not seen by pytest_lazyfixture unless autouse=True

Open wikiped opened this issue 7 years ago • 0 comments

Using modified example from the official Usage instructions:

@fixture(params=[0, 1], autouse=True)
def zero_one(request):
    return request.param

@fixture(params=[1])
def one(request, zero_one):
    return zero_one * request.param

@fixture(params=[0, 1], autouse=False)
def zero_two(request):
    return request.param

@fixture
def two(request, zero_two):
    return zero_two * request.param

@fixture(params=[
    lazy_fixture('one'),
])
def some_auto(request):
    return request.param

@fixture(params=[
    lazy_fixture('two')
])
def some(request):
    return request.param

def test_pytest_can_see_all_fixtures(zero_one, zero_two, one, two):
    for f in (zero_one, zero_two, one, two):
        assert isinstance(f, int)

def test_func_autouse(some_auto):
    assert some_auto in [0, 1]

def test_func_no_autouse(some):
    assert some in [0, 1]

The last test will fail unexpectedly with error:

C:\Anaconda\lib\site-packages\_pytest\python.py:680: ValueError

During handling of the above exception, another exception occurred:

request = <FixtureRequest for <Function 'test_func_no_autouse[0-some0]'>>

    def fill(request):
        item = request._pyfuncitem
        fixturenames = item.fixturenames
        argnames = item._fixtureinfo.argnames
    
        for fname in fixturenames:
            if fname not in item.funcargs and fname not in argnames:
                item.funcargs[fname] = request.getfixturevalue(fname)
    
        if hasattr(item, 'callspec'):
            for param, val in sorted_by_dependency(item.callspec.params, fixturenames):
                if is_lazy_fixture(val):
>                   item.callspec.params[param] = request.getfixturevalue(val.name)
E                   Failed: The requested fixture has no parameter defined for the current test.
E                   
E                   Requested fixture 'zero_two' defined in:
E                   some_package\tests\test_example.py:47
E                   
E                   Requested here:
E                   C:\Anaconda\lib\site-packages\_pytest\fixtures.py:523

C:\Anaconda\lib\site-packages\pytest_lazyfixture.py:40: Failed

For some reason pytest_lazyfixture doesn't see fixtures that depend on other fixtures that pytest can see unless autouse=True in the lowest level fixture.

wikiped avatar Feb 05 '18 14:02 wikiped