question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
muvstercommented, Oct 8, 2018

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.

4reactions
gimbocommented, Apr 23, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use pytest.lazy_fixture list values as parameters in another ...
See this answer to a very similar question: by design, fixture values can not be used as lists of parameters to parametrize tests....
Read more >
Welcome to pytest-factoryboy's documentation! — pytest ...
Library exports a function to register factories as fixtures. Fixtures are contributed to the same module where register function is called. Factory Fixture¶....
Read more >
pytest Documentation
2.3.14 Use fixtures in classes and modules with usefixtures. Sometimes test functions do not directly need access to a fixture object.
Read more >
pytest-lazy-fixture
Project description. Use your fixtures in @pytest.mark.parametrize. Installation. pip install pytest-lazy-fixture ...
Read more >
Deep dive into Pytest parametrization | by George Shuklin
If a few fixtures are used in one test function, pytest generates a Cartesian product of ... This function is not a fixture,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found