TvoroG / pytest-lazy-fixture

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

TypeError: 'LazyFixture' object is not iterable #56

Open ChristianF88 opened 2 years ago

ChristianF88 commented 2 years ago

Hi there,

I am running into issues with using a fixture, that loads a list of parameters that I want to use to parametrize tests. I am not sure, whether that is something, that can be done with your package and I just can't figure it out, or whether that would be a new feature.

Here is a example on what I want to achieve:

import pytest

@pytest.fixture()
def one(tmp_path):
    return list(str(tmp_path))

@pytest.mark.parametrize("char", pytest.lazy_fixture('one'))
def test_func(char):
    assert char.isascii()

Do you have an idea on how to get my code running?

Thanks a lot!

ChristianF88 commented 2 years ago

So it seems like I am not the only one in demand of this feature. Any plans on whether that could be implemented at some point?

notamonad commented 2 years ago

+1 for this feature request

djhoese commented 2 years ago

I found this issue while looking for something else. It isn't what I'm looking for, but now I'm curious by your problem. You want the fixture to be the list of parameters? I think normally you would do the parametrization on the fixture itself and then use that fixture in the test function. This is kind of shown in the README of this project:

import pytest
from pytest_lazyfixture import lazy_fixture

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

@pytest.mark.parametrize('arg1,arg2', [
    ('val1', lazy_fixture('one')),
])
def test_func(arg1, arg2):
    assert arg2 in [1, 2]

But also talked about in the pytest docs here:

https://docs.pytest.org/en/6.2.x/fixture.html#parametrizing-fixtures