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')