diff --git a/src/_pytest/python_api.py b/src/_pytest/python_api.py index 7c63a3588..aae5ced33 100644 --- a/src/_pytest/python_api.py +++ b/src/_pytest/python_api.py @@ -653,9 +653,12 @@ def raises(expected_exception, *args, match=None, **kwargs): message = "DID NOT RAISE {}".format(expected_exception) if not args: - return RaisesContext( - expected_exception, message=message, match_expr=match, **kwargs - ) + if kwargs: + msg = "Unexpected keyword arguments passed to pytest.raises: " + msg += ", ".join(sorted(kwargs)) + msg += "\nUse context-manager form instead?" + raise TypeError(msg) + return RaisesContext(expected_exception, message, match) else: func = args[0] if not callable(func): diff --git a/src/_pytest/recwarn.py b/src/_pytest/recwarn.py index b124c69d5..7e772aa35 100644 --- a/src/_pytest/recwarn.py +++ b/src/_pytest/recwarn.py @@ -76,7 +76,12 @@ def warns(expected_warning, *args, match=None, **kwargs): """ __tracebackhide__ = True if not args: - return WarningsChecker(expected_warning, match_expr=match, **kwargs) + if kwargs: + msg = "Unexpected keyword arguments passed to pytest.warns: " + msg += ", ".join(sorted(kwargs)) + msg += "\nUse context-manager form instead?" + raise TypeError(msg) + return WarningsChecker(expected_warning, match_expr=match) else: func = args[0] if not callable(func): diff --git a/testing/python/raises.py b/testing/python/raises.py index 1f5594c8a..668be57fc 100644 --- a/testing/python/raises.py +++ b/testing/python/raises.py @@ -248,3 +248,9 @@ class TestRaises: with pytest.raises(CrappyClass()): pass assert "via __class__" in excinfo.value.args[0] + + def test_raises_context_manager_with_kwargs(self): + with pytest.raises(TypeError) as excinfo: + with pytest.raises(Exception, foo="bar"): + pass + assert "Unexpected keyword arguments" in str(excinfo.value) diff --git a/testing/test_recwarn.py b/testing/test_recwarn.py index 65fdd1682..208dc5b44 100644 --- a/testing/test_recwarn.py +++ b/testing/test_recwarn.py @@ -374,3 +374,9 @@ class TestWarns: assert f() == 10 assert pytest.warns(UserWarning, f) == 10 assert pytest.warns(UserWarning, f) == 10 + + def test_warns_context_manager_with_kwargs(self): + with pytest.raises(TypeError) as excinfo: + with pytest.warns(UserWarning, foo="bar"): + pass + assert "Unexpected keyword arguments" in str(excinfo.value)