'Is there a way to parameterize expected values that require evaluation of the param?

This is pseudo code for what I'd like to do:

@pytest.mark.localtest
@pytest.mark.parametrize(
    "input,expected",
    [
        # should generate a random number greater than a specific value
        ("RANDOM_NUMBER", lambda input: ge(input, 10000000)),
        # should generate a random string longer than a specific value
        ("RANDOM_STRING", lambda input: len(input) == 10,
    ],
)

def test__my_func(input, expected):
    assert my_func.(input) == expected(input)

Is it possible to have the "expected" value be an expression against the input? This would be useful for a function like this one I'm testing that generates semi-nondeterministic output.

EDIT

This actually works. I might close this, but curious if there is a more builtin way in pytest.

@pytest.mark.localtest
@pytest.mark.parametrize(
    "input,expected",
    [
        # should generate a random number greater than a specific value
        ("RANDOM_NUMBER", lambda input: ge(input, 10000000)),
    ],
)

def test__my_func(input, expected):
    assert expected(my_func.(input))


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source