Handle match with pytest.raises() (#6753)

Fixes https://github.com/pytest-dev/pytest/issues/6752.
This commit is contained in:
Daniel Hahler
2020-02-22 23:32:55 +01:00
committed by GitHub
parent 68fe0eb8f3
commit c8b4a1a471
3 changed files with 32 additions and 9 deletions

View File

@@ -557,18 +557,16 @@ def raises( # noqa: F811
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
func: Callable,
*args: Any,
match: Optional[str] = ...,
**kwargs: Any
) -> Optional[_pytest._code.ExceptionInfo[_E]]:
) -> _pytest._code.ExceptionInfo[_E]:
... # pragma: no cover
def raises( # noqa: F811
expected_exception: Union["Type[_E]", Tuple["Type[_E]", ...]],
*args: Any,
match: Optional[Union[str, "Pattern"]] = None,
**kwargs: Any
) -> Union["RaisesContext[_E]", Optional[_pytest._code.ExceptionInfo[_E]]]:
) -> Union["RaisesContext[_E]", _pytest._code.ExceptionInfo[_E]]:
r"""
Assert that a code block/function call raises ``expected_exception``
or raise a failure exception otherwise.
@@ -579,8 +577,12 @@ def raises( # noqa: F811
string that may contain `special characters`__, the pattern can
first be escaped with ``re.escape``.
__ https://docs.python.org/3/library/re.html#regular-expression-syntax
(This is only used when ``pytest.raises`` is used as a context manager,
and passed through to the function otherwise.
When using ``pytest.raises`` as a function, you can use:
``pytest.raises(Exc, func, match="passed on").match("my pattern")``.)
__ https://docs.python.org/3/library/re.html#regular-expression-syntax
.. currentmodule:: _pytest._code
@@ -684,6 +686,7 @@ def raises( # noqa: F811
message = "DID NOT RAISE {}".format(expected_exception)
if not args:
match = kwargs.pop("match", None)
if kwargs:
msg = "Unexpected keyword arguments passed to pytest.raises: "
msg += ", ".join(sorted(kwargs))