Merge pull request #3349 from jeffreyrack/3348-unknown-argument

3348: raise error on unknown arguments to raises
This commit is contained in:
Ronny Pfannschmidt 2018-03-28 21:45:59 +02:00 committed by GitHub
commit 2efaf39ed8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 1 deletions

View File

@ -597,6 +597,10 @@ def raises(expected_exception, *args, **kwargs):
message = kwargs.pop("message")
if "match" in kwargs:
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)
elif isinstance(args[0], str):
code, = args

View File

@ -0,0 +1 @@
``pytest.raises`` now raises ``TypeError`` when receiving an unknown keyword argument.

View File

@ -61,6 +61,11 @@ class TestRaises(object):
with pytest.raises(TypeError):
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):
with pytest.raises((KeyError, ValueError)):
raise KeyError('oops')

View File

@ -113,7 +113,7 @@ class TestDeprecatedCall(object):
pass
msg = 'Did not produce DeprecationWarning or PendingDeprecationWarning'
with pytest.raises(AssertionError, matches=msg):
with pytest.raises(AssertionError, match=msg):
if mode == 'call':
pytest.deprecated_call(f)
else: