Skip to content
Advertisement

Testing the same function with different parameters

I am trying to test a function multiple times using different parameters. The return value should be True.

def testConfiguration(small,medium,large):
    ...
    if (everything goes well):
        return True
    else:
        return False

testConfiguration(0,0,1)
testConfiguration(1,2,1)
testConfiguration(1,3,1)

What’s the best way to go about doing this in pytest? I want to avoid multiple functions acting as assert True wrappers e.g

def test_ConfigA():
    assert testConfiguration(0,0,1) == True

def test_ConfigB():
    assert testConfiguration(1,2,1) == True

...

Advertisement

Answer

Use @pytest.mark.parametrize()

@pytest.mark.parametrize("small, medium, large, out", [
    (0,1,1, True),
    (1,2,1, True),
    (1,1,1, False),
])
def test_all(small, medium, large, out):
     assert testConfiguration(small, medium, large) == out
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement