Pytest parametrize passing function name as a parameter

865 views Asked by At

I am trying to get function name as a parameter in pytest parametrize but getting the error 'str' object is not callable since it passes as a string.

@pytest.fixture(autouse=True)
def mock_something(mocker, function_name, value):
    return mocker.patch(function_name, return_value = value)

@pytest.mark.parametrize(
    "expected, value, function_name", 
    [(100001, 100008, "one_adder")],
)
def test_create_uniq_no(expected, function_name, value):
    assert expected == function_name(value)

The patch part is working when i write the function name manually in mocker.patch, but that would require me to write every function/class name in order to test them. How can i have the function name as a parameter?

I tried using globals but got a key error.

@pytest.fixture(autouse=True)
def mock_something(mocker, function_name, value):
    return mocker.patch(function_name, return_value = value)

@pytest.mark.parametrize(
    "expected, value, function_name", 
    [(100001, 100008, "one_adder")],
)
def test_some_func(expected, function_name, value):
    func = globals()[function_name]
    assert expected == func(value)
0

There are 0 answers