Last active
August 5, 2021 09:48
-
-
Save aplz/32c499bccef95ffb8d3008ab7026fb8e to your computer and use it in GitHub Desktop.
assert that some input results in an exception being raised
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from typing import List | |
def method_under_test(values: List[int]) -> int: | |
"""dummy method""" | |
if 0 in values: | |
raise(ValueError("Zeros are not supported")) | |
return sum(values) | |
@pytest.mark.parametrize( | |
"flat,expected", | |
[ | |
... | |
([0,2,3], ValueError), | |
([1,2,3], 6), | |
... , | |
], | |
) | |
def test_collapse(value, expected): | |
if type(expected) == type and issubclass(expected, Exception): | |
with pytest.raises(expected): | |
method_under_test(value) | |
else: | |
assert method_under_test(value) == expected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment