improve `pytest.raises`
This commit is contained in:
parent
e580534df0
commit
3444d35c54
1
AUTHORS
1
AUTHORS
|
@ -44,6 +44,7 @@ Aron Coyle
|
|||
Aron Curzon
|
||||
Aviral Verma
|
||||
Aviv Palivoda
|
||||
Babak Keyvani
|
||||
Barney Gale
|
||||
Ben Gartner
|
||||
Ben Webb
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Improve :py:func:`pytest.raises`. Previously passing an empty tuple would give a confusing
|
||||
error. We now raise immediately with a more helpful message.
|
|
@ -899,6 +899,13 @@ def raises(
|
|||
"""
|
||||
__tracebackhide__ = True
|
||||
|
||||
if 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'."
|
||||
)
|
||||
if isinstance(expected_exception, type):
|
||||
excepted_exceptions: Tuple[Type[E], ...] = (expected_exception,)
|
||||
else:
|
||||
|
|
|
@ -19,6 +19,10 @@ class TestRaises:
|
|||
excinfo = pytest.raises(ValueError, int, "hello")
|
||||
assert "invalid literal" in str(excinfo.value)
|
||||
|
||||
def test_raises_does_not_allow_empty_tuple(self):
|
||||
with pytest.raises(ValueError):
|
||||
pytest.raises(expected_exception=())
|
||||
|
||||
def test_raises_callable_no_exception(self) -> None:
|
||||
class A:
|
||||
def __call__(self):
|
||||
|
|
Loading…
Reference in New Issue