Reputation: 440
I'm trying to test code where I want to test multiple rules within a single with pytest.raises(ValueError) exception, is there a Pythonic way to do this? In the example below, I want to test that all 4 function calls will throw the value error.
With pytest.raises(ValueError):
function_that_throws_exception(param1)
function_that_throws_exception(param2)
function_that_throws_exception(param3)
function_that_throws_exception(param4)
Upvotes: 0
Views: 2357
Reputation: 1275
I'd suggest to use parametrize
:
@pytest.mark.parametrize("param", [param1, param2...])
def test_function_that_throws_exception(param):
with pytest.raises(ValueError):
function_that_throws_exception(param)
Upvotes: 5
Reputation: 9963
You could do something like:
params_that_should_throw = [param1, param2, param3, param4]
for param in params_that_should_throw:
with pytest.raises(ValueError):
function_that_throws_exception(param)
Upvotes: 0