Is it possible to return a list from fixture to parametrize in pytest?

37 views Asked by At

I have a code where in the fixture there will be authorization, sending a request, receiving a response, getting a list from the response. The result will be just a list with the values that I need to pass from the fixture for parameterization in many tests. Please tell me, is it possible? For example, I specified a predefined list, but the values in it will be changed, it will depend on the response to the request:

import pytest
import requests


@pytest.fixture()
def conf_response():
    request_list = [1, 2, 2]
    return request_list


@pytest.mark.parametrize(
    "a",
    [
        (pytest.lazy_fixture("conf_response")),
    ],
)
def test_one(a):
    assert a > 0

@pytest.mark.parametrize(
    "a",
    [
        (pytest.lazy_fixture("conf_response")),
    ],
)
def test_two(a):
    assert a == 0

I tried use

pytest.lazy_fixture

But it does not take values from the list for tests, it takes the whole . Also tried pytest-cases but he also does not to work with list.

1

There are 1 answers

0
Teejay Bruno On BEST ANSWER

I would recommend parametrizing the fixture instead of the test cases.

Consider the following example --

import pytest


def get_params() -> list:
    return [1, 2, 3]

@pytest.fixture(params=get_params())
def conf_response(request):
    yield request.param

def test_one(conf_response):
    assert conf_response > 0

def test_two(conf_response):
    assert conf_response == 0