Parametrize the parameter name in a call

Viewed 26

I am working on testing (using pytest) of a number of related methods with a similar but not completely uniform API. The critical difference at this point is a required parameter-name difference between two types of API:

# API 1
def action1(*, info, provider)

# API2
def action2(*, info, source)

I can parametrize a single test to handle both of these types identically, but I don't see a way to use the API parameter name -- provider vs source -- that could be provided by the test parameters. Is this possible?

The alternative is to pass a wrapper function with a parameter set that remaps to the individual APIs, but I already have a lot of indirection and I would rather not add more.

1 Answers

If I'm understanding your question correctly, you can parametrize your test with (i) the function you want to call and (ii) a dictionary containing any keyword arguments you want to call that function with. For example:

import pytest

def action1(*, info, provider):
    return info, provider

def action2(*, info, source):
    return info, source

@pytest.mark.parametrize(
        'f, kwargs, expected', [
            (action1, dict(info=1, provider=2), (1, 2)),
            (action2, dict(info=3, source=4), (3, 4)),
        ]
)
def test_actions(f, kwargs, expected):
    assert f(**kwargs) == expected
Related