improve `pytest.raises` - cont'd

a few more iterations on error message and related tests.
This commit is contained in:
Babak Keyvani 2022-05-09 21:15:02 -04:00
parent 3444d35c54
commit 31a9c5c667
2 changed files with 9 additions and 6 deletions

View File

@ -899,12 +899,11 @@ def raises(
"""
__tracebackhide__ = True
if expected_exception == ():
if not expected_exception:
raise ValueError(
"Passing expected_exception=() is an error, because it's impossible to "
"raise an exception which is not an instance of any type. Raising exceptions "
"is already understood as failing the test, so you don't need any special "
"code to say 'this should never raise an exception'."
f"Expected an exception type or a tuple of exception types, but got `{expected_exception!r}`. "
f"Raising exceptions is already understood as failing the test, so you don't need "
f"any special code to say 'this should never raise an exception'."
)
if isinstance(expected_exception, type):
excepted_exceptions: Tuple[Type[E], ...] = (expected_exception,)

View File

@ -19,8 +19,12 @@ class TestRaises:
excinfo = pytest.raises(ValueError, int, "hello")
assert "invalid literal" in str(excinfo.value)
def test_raises_does_not_allow_none(self):
with pytest.raises(ValueError, match="Expected an exception type or"):
pytest.raises(expected_exception=None)
def test_raises_does_not_allow_empty_tuple(self):
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Expected an exception type or"):
pytest.raises(expected_exception=())
def test_raises_callable_no_exception(self) -> None: