TvoroG / pytest-lazy-fixture

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

Add support for nested lazy_fixtures calls #28

Open bigbZik opened 6 years ago

bigbZik commented 6 years ago

Example:

@pytest.fixture(scope="class")
def fix1():
    return "fix1_result"

@pytest.fixture(scope="function")
def some_fixture0():
    yield pytest.lazy_fixture("fix1")

@pytest.fixture(scope="function")
def some_fixture1(some_fixture0):
    yield "some_" + some_fixture0

@pytest.fixture(scope="function")
def some_fixture2(some_fixture1):
    yield "more_" + some_fixture1

def test1(some_fixture2):
    assert some_fixture2 == "more_some_fix1_result"

Example 2:

@pytest.fixture
def one(request):
    return "SOME_VALUE"

@pytest.fixture
def two(request):
    return "SOME_VALUE2"

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

@pytest.fixture
def anoter_fixture2(anoter_fixture1):
    return "NEW_" + anoter_fixture1

def test2(anoter_fixture2):
    assert ((anoter_fixture2 == "NEW_SOME_VALUE") or (anoter_fixture2 == "NEW_SOME_VALUE2"))
bigbZik commented 6 years ago

Do you have any comments?

TvoroG commented 6 years ago

@bigbZik, Hi! Thanks for contribution! I'll check it out for sure on this week. Sorry for not answering)

TvoroG commented 6 years ago

Hey! Thanks again, but I'm not sure this function is the right way to solve the problem:

def get_fixture_value(fname, request):
    res = request.getfixturevalue(fname)
    while is_lazy_fixture(res):
        res = request.getfixturevalue(res.name)
    return res

I was able to solve first test only for now. Check out https://github.com/TvoroG/pytest-lazy-fixture/tree/pull28.

I will need more time for test_lazy_fixture_nested_fixtures_in_parametrize test.