Update pytest.raises to raise a TypeError when an invalid keyword argument is passed.
This commit is contained in:
parent
ff3d13ed0e
commit
34afded06d
|
@ -597,6 +597,10 @@ def raises(expected_exception, *args, **kwargs):
|
||||||
message = kwargs.pop("message")
|
message = kwargs.pop("message")
|
||||||
if "match" in kwargs:
|
if "match" in kwargs:
|
||||||
match_expr = kwargs.pop("match")
|
match_expr = kwargs.pop("match")
|
||||||
|
if len(kwargs.keys()) > 0:
|
||||||
|
msg = 'Unexpected keyword arguments passed to pytest.raises: '
|
||||||
|
msg += ', '.join(kwargs.keys())
|
||||||
|
raise TypeError(msg)
|
||||||
return RaisesContext(expected_exception, message, match_expr)
|
return RaisesContext(expected_exception, message, match_expr)
|
||||||
elif isinstance(args[0], str):
|
elif isinstance(args[0], str):
|
||||||
code, = args
|
code, = args
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Updated `pytest.raises` to raise a TypeError when an invalid keyword argument is passed.
|
|
@ -61,6 +61,11 @@ class TestRaises(object):
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
pytest.raises('wrong', lambda: None)
|
pytest.raises('wrong', lambda: None)
|
||||||
|
|
||||||
|
def test_invalid_arguments_to_raises(self):
|
||||||
|
with pytest.raises(TypeError, match='unknown'):
|
||||||
|
with pytest.raises(TypeError, unknown='bogus'):
|
||||||
|
raise ValueError()
|
||||||
|
|
||||||
def test_tuple(self):
|
def test_tuple(self):
|
||||||
with pytest.raises((KeyError, ValueError)):
|
with pytest.raises((KeyError, ValueError)):
|
||||||
raise KeyError('oops')
|
raise KeyError('oops')
|
||||||
|
|
Loading…
Reference in New Issue