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)
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)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.