[Python] How to concatenate several parametrized fixtures into a new fixture in py.test?

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    If I have two parametrized fixtures, how can I create a single test function that is called first with the instances of one fixture and then with the instances of the other fixture?

    I guess it would make sense to create a new fixture that somehow concatenates the two existing fixtures. This works well for "normal" fixtures, but I don't seem to get it to work with parametrized fixtures.

    Here is a simplified example of what I tried:

    import pytest

    @pytest.fixture(params=[1, 2, 3])
    def lower(request):
    return "i" * request.param

    @pytest.fixture(params=[1, 2])
    def upper(request):
    return "I" * request.param

    @pytest.fixture(params=['lower', 'upper'])
    def all(request):
    return request.getfuncargvalue(request.param)

    def test_all(all):
    assert 0, all


    When I run this I get this error:

    request = <SubRequest 'lower' for <Function 'test_all[lower]'>>

    @pytest.fixture(params=[1, 2, 3])
    def lower(request):
    > return "i" * request.param
    E AttributeError: 'SubRequest' object has no attribute 'param'


    ... and the same error for upper().

    What did I do wrong?

    How can I fix this?

    Continue reading...

Compartilhe esta Página