Skip to content

Instantly share code, notes, and snippets.

@aplz
Last active August 5, 2021 09:48
Show Gist options
  • Save aplz/32c499bccef95ffb8d3008ab7026fb8e to your computer and use it in GitHub Desktop.
Save aplz/32c499bccef95ffb8d3008ab7026fb8e to your computer and use it in GitHub Desktop.
assert that some input results in an exception being raised
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