Search code examples
pythonpytestfixtures

Parameterizing fixtures not working on Pytest


I have already used params on fixtures (@pytest.fixture(params=params)) to run the same test with several test cases. In this case, I think I'm doing exactly the same but this time the fixture doesn't return anything. What is strange is that the test runs as many times as many parameters I passed to the function.

So, my sample code:

def double(n):
    return 2 * n


TEST_CASES_DOUBLE = [
    #(input, expected)
    (1, 2),
    (2, 4),
    (3, 6),
]

# Fixtures


@pytest.fixture(params=TEST_CASES_DOUBLE)
def params_function_double(request):
    request.param


# Tests


def test_double(params_function_double):
    param, expected = params_function_double
    result = double(param)
    assert result == expected

And I get the errors:

======================================================================================= FAILURES ========================================================================================
_________________________________________________________________________ test_double[params_function_double0] __________________________________________________________________________

params_function_double = None

    def test_double(params_function_double):
>       param, expected = params_function_double
E       TypeError: cannot unpack non-iterable NoneType object

test.py:56: TypeError
_________________________________________________________________________ test_double[params_function_double1] __________________________________________________________________________

params_function_double = None

    def test_double(params_function_double):
>       param, expected = params_function_double
E       TypeError: cannot unpack non-iterable NoneType object

test.py:56: TypeError
_________________________________________________________________________ test_double[params_function_double2] __________________________________________________________________________

params_function_double = None

    def test_double(params_function_double):
>       param, expected = params_function_double
E       TypeError: cannot unpack non-iterable NoneType object

test.py:56: TypeError
================================================================================ short test summary info ================================================================================
FAILED test.py::test_double[params_function_double0] - TypeError: cannot unpack non-iterable NoneType object
FAILED test.py::test_double[params_function_double1] - TypeError: cannot unpack non-iterable NoneType object
FAILED test.py::test_double[params_function_double2] - TypeError: cannot unpack non-iterable NoneType object

Solution

  • The main issue is that your params_function_double is not returning anything, hence why you get a NoneType error.

    What you may be looking for is not the fixture decorator but rather the pytest.mark.parametrize decorator (see this page). Here's how the test implementation would look like with it

    def double(n):
        return 2 * n
    
    # Tests
    @pytest.mark.parametrize(
        "input_, expected",
        [(1, 2), (2, 4), (3, 6)]
    )
    def test_double(input_, expected):
        result = double(input_)
        assert result == expected