summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeffrey Rackauckas <jeffreyrack@gmail.com>2018-03-27 19:57:15 -0700
committerJeffrey Rackauckas <jeffreyrack@gmail.com>2018-03-27 19:57:15 -0700
commit34afded06dbcc4a2615043c3deaea64511d4287d (patch)
tree0fac56770a4908f04edd4c6aa72a5daadea168ca
parentff3d13ed0efab6692a07059b1d61c53eec6e0412 (diff)
downloadpytest-34afded06dbcc4a2615043c3deaea64511d4287d.tar.gz
Update pytest.raises to raise a TypeError when an invalid keyword argument is passed.
-rw-r--r--_pytest/python_api.py4
-rw-r--r--changelog/3348.bugfix.rst1
-rw-r--r--testing/python/raises.py5
3 files changed, 10 insertions, 0 deletions
diff --git a/_pytest/python_api.py b/_pytest/python_api.py
index 69ae6ed0e..5e4776ce3 100644
--- a/_pytest/python_api.py
+++ b/_pytest/python_api.py
@@ -597,6 +597,10 @@ def raises(expected_exception, *args, **kwargs):
message = kwargs.pop("message")
if "match" in kwargs:
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)
elif isinstance(args[0], str):
code, = args
diff --git a/changelog/3348.bugfix.rst b/changelog/3348.bugfix.rst
new file mode 100644
index 000000000..7b56ebf98
--- /dev/null
+++ b/changelog/3348.bugfix.rst
@@ -0,0 +1 @@
+Updated `pytest.raises` to raise a TypeError when an invalid keyword argument is passed. \ No newline at end of file
diff --git a/testing/python/raises.py b/testing/python/raises.py
index 183259f6b..156319816 100644
--- a/testing/python/raises.py
+++ b/testing/python/raises.py
@@ -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')