File tree Expand file tree Collapse file tree 4 files changed +19
-0
lines changed Expand file tree Collapse file tree 4 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ Aron Coyle
44
44
Aron Curzon
45
45
Aviral Verma
46
46
Aviv Palivoda
47
+ Babak Keyvani
47
48
Barney Gale
48
49
Ben Gartner
49
50
Ben Webb
Original file line number Diff line number Diff line change
1
+ Improve :py:func: `pytest.raises `. Previously passing an empty tuple would give a confusing
2
+ error. We now raise immediately with a more helpful message.
Original file line number Diff line number Diff line change @@ -899,6 +899,12 @@ def raises(
899
899
"""
900
900
__tracebackhide__ = True
901
901
902
+ if not expected_exception :
903
+ raise ValueError (
904
+ f"Expected an exception type or a tuple of exception types, but got `{ expected_exception !r} `. "
905
+ f"Raising exceptions is already understood as failing the test, so you don't need "
906
+ f"any special code to say 'this should never raise an exception'."
907
+ )
902
908
if isinstance (expected_exception , type ):
903
909
excepted_exceptions : Tuple [Type [E ], ...] = (expected_exception ,)
904
910
else :
Original file line number Diff line number Diff line change @@ -19,6 +19,16 @@ def test_raises_function(self):
19
19
excinfo = pytest .raises (ValueError , int , "hello" )
20
20
assert "invalid literal" in str (excinfo .value )
21
21
22
+ def test_raises_does_not_allow_none (self ):
23
+ with pytest .raises (ValueError , match = "Expected an exception type or" ):
24
+ # We're testing that this invalid usage gives a helpful error,
25
+ # so we can ignore Mypy telling us that None is invalid.
26
+ pytest .raises (expected_exception = None ) # type: ignore
27
+
28
+ def test_raises_does_not_allow_empty_tuple (self ):
29
+ with pytest .raises (ValueError , match = "Expected an exception type or" ):
30
+ pytest .raises (expected_exception = ())
31
+
22
32
def test_raises_callable_no_exception (self ) -> None :
23
33
class A :
24
34
def __call__ (self ):
You can’t perform that action at this time.
0 commit comments