Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameterize a pytest with functions

Tags:

python

pytest

I have a number of different custom transformer functions, and want to run a simple test (e.g. printing out the transformer) on all of these functions. I could write individual tests for each function, but thought there must be an easier way.

I've looked at parameterizing the test but get: TypeError: 'function' object is not iterable

@pytest.fixture
def list_transformers(self):
    list_transformers = [
        TransformerOne(column='a'),
        TransformerTwo(column='a')
    ]
    return list_trasformers

@pytest.mark.parametrize("transformer", list_transformers)
    def test_print(self, transformer):
        tf_type = transformer[0]
        params = transformer[1]
        tf = tf_type(**params)
        print(tf)

I think this is because the transformer functions are not returning something iterable. Should I be going about this in a different way?

like image 283
LAS Avatar asked Nov 17 '25 05:11

LAS


2 Answers

Using fixtures in pytest.mark.parametrize is not yet fully supported.

Instead of using @pytest.mark.parametrize, you can use another style of parametrized tests which is via pytest_generate_tests.

import pytest


def pytest_generate_tests(metafunc):
    if "transformer" in metafunc.fixturenames:
        list_transformers = [
            'This is a',
            'While that is b',
        ]
        metafunc.parametrize("transformer", list_transformers)


def test_print(transformer):
    print(transformer)

Output:

$ pytest -q -rP
..                                                                                                                                                                                                  [100%]
================================================================================================= PASSES ==================================================================================================
__________________________________________________________________________________________ test_print[This is a] __________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
This is a
_______________________________________________________________________________________ test_print[While that is b] _______________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
While that is b
2 passed in 0.07s
like image 159
Niel Godfrey Ponciano Avatar answered Nov 19 '25 20:11

Niel Godfrey Ponciano


The second parameter of pytest.mark.parametrize() have to be an iterator not a function reference.

Something like this: @pytest.mark.parametrize("transformer", list_transformers())

or @pytest.mark.parametrize("transformer", [TransformerOne(column='a'), TransformerTwo(column='a')])

like image 28
Rukamakama Avatar answered Nov 19 '25 20:11

Rukamakama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!