TvoroG / pytest-lazy-fixture

It helps to use fixtures in pytest.mark.parametrize
MIT License
379 stars 30 forks source link

LazyFixture only resolved when used directly, but not in lists etc. #24

Open cb109 opened 6 years ago

cb109 commented 6 years ago

My use case requires preparing lists of things instead of just single things one by one, please see your example adapted accordingly below.

import pytest

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

@pytest.fixture
def one():
    return 1

@pytest.fixture
def two():
    return 2

def test_func(some_list):
    from pprint import pprint
    pprint(some_list)
    assert 1 in some_list

Inside test_func() the some_list will be something like [<LazyFixture "one">, <LazyFixture "two">] where I would have expected that this had been expanded/resolved to [1, 2].

Is there a way to always expand /resolve in a test context no matter how wrapped or nested the LazyFixture is?

Cheers

TvoroG commented 6 years ago

Hi, @cb109! For now it's not possible to expand lazy fixture inside of nested objects. Here is a similar issue.

I will not close it, because it's an issue anyway. Thanks!

muvster commented 6 years ago

I'd love to see this supported as well. We have a case where we want to add some metadata about existing fixtures when they are used as part of a different fixture. The metadata doesn't make sense to include in the original fixtures because it is only relevant in the new context. The natural way to tie the original fixtures and related metadata together would be in the params of the new fixture.

gimbo commented 4 years ago

Another place where this shows up is parametrized tests which operate over arbitrary numbers of arguments, which is something I often use.

Toy example:

@pytest.mark.parametrize(
    'args',
    (
        (),
        (None, None, None),
        (lazy_fixture('some_fixture'), 12.5, 'foo', ['hello world']),
    ),
)
def test_with_arbitrary_list_of_args(args):
    # Do something with args here...

Here on the third run of the test, args[0] will be <LazyFixture "some_fixture"> rather than the result of that fixture; to get the actual result I am forced to use a fixed number of explicitly-named arguments, it seems.

blaise17 commented 3 years ago

I also have this issue. My use case is a single test that checks that all the methods that should raise a certain exception do, so in my parametrize I pass in the Callable as one argument and the "extra parameters" dictionary as the second. I then expand this to pass them in as keyword args to the Callable, but one of these extra parameters, for one method only, needs to use a fixture (as the exception is only thrown when all other params are valid).

z33kz33k commented 2 years ago

Count me also among those who consider this issue important.

KerH commented 2 years ago

I tackled it as well, in my case I wanted to pass a dictionary with multiple fixtures. Since it was passed as a sequence I don't have the access to the fixture value within. +1 for this request.

codectl commented 2 years ago

This feature would be useful. But since there is no plans to release this, I am moving to pytest-cases.

vipese-idoven commented 2 months ago

Finding this important as well!

My case, inside a dictionary:

@pytest.mark.parametrize(
    argnames=["args", "error"],
    argvalues=[
        ({"filename": "1000"}, TypeError),
        ({"filename": pytest.lazy_fixture("filename"), "threshold": "threshold"}, TypeError),
        ({"filename": pytest.lazy_fixture("no_filename")}, FileNotFoundError),
        ({"filename": pytest.lazy_fixture("filename"), "threshold": -10}, ValueError),
        ({"filename": pytest.lazy_fixture("filename"), "threshold": 1}, ValueError),
        ({"filename": pytest.lazy_fixture("filename"), "threshold": 10}, ValueError),
    ]
)
def test_ibd_errors(  # noqa: D103
    otter_qc: OtterQC, args: dict[str, Any], error: Exception
):
    with pytest.raises(error):
        otter_qc.ibd(**args)