Merge pull request #3349 from jeffreyrack/3348-unknown-argument
3348: raise error on unknown arguments to raises
This commit is contained in:
commit
2efaf39ed8
|
@ -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 kwargs:
|
||||||
|
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 @@
|
||||||
|
``pytest.raises`` now raises ``TypeError`` when receiving an unknown keyword argument.
|
|
@ -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')
|
||||||
|
|
|
@ -113,7 +113,7 @@ class TestDeprecatedCall(object):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
msg = 'Did not produce DeprecationWarning or PendingDeprecationWarning'
|
msg = 'Did not produce DeprecationWarning or PendingDeprecationWarning'
|
||||||
with pytest.raises(AssertionError, matches=msg):
|
with pytest.raises(AssertionError, match=msg):
|
||||||
if mode == 'call':
|
if mode == 'call':
|
||||||
pytest.deprecated_call(f)
|
pytest.deprecated_call(f)
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue