File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -179,6 +179,7 @@ Fraser Stark
179179Freya Bruhin
180180Gabriel Landau
181181Gabriel Reis
182+ Garion Milazzo
182183Garvit Shubham
183184Gene Wood
184185George Kussumoto
Original file line number Diff line number Diff line change 1+ Improved :func: `pytest.raises(..., match=...) <pytest.raises> ` failures to suppress the mismatched exception as a cause of the resulting ``AssertionError ``.
Original file line number Diff line number Diff line change @@ -716,7 +716,7 @@ def __exit__(
716716 if not self .matches (exc_val ):
717717 if self ._just_propagate :
718718 return False
719- raise AssertionError (self ._fail_reason )
719+ raise AssertionError (self ._fail_reason ) from None
720720
721721 # Cast to narrow the exception type now that it's verified....
722722 # even though the TypeGuard in self.matches should be narrowing
Original file line number Diff line number Diff line change @@ -177,6 +177,33 @@ def test_invalid_regex():
177177 result .stdout .no_fnmatch_line ("*File*" )
178178 result .stdout .no_fnmatch_line ("*line*" )
179179
180+ def test_raises_match_failure_suppresses_exception_context (
181+ self , pytester : Pytester
182+ ) -> None :
183+ pytester .makepyfile (
184+ """
185+ import pytest
186+
187+ def test_raises_match_failure():
188+ with pytest.raises(ValueError, match="expected"):
189+ raise ValueError("actual")
190+ """
191+ )
192+ result = pytester .runpytest ("--tb=short" )
193+ assert result .ret == 1
194+ result .stdout .fnmatch_lines (
195+ [
196+ "*E*AssertionError: Regex pattern did not match.*" ,
197+ ]
198+ )
199+ result .stdout .no_fnmatch_line ("*ValueError: actual" )
200+ result .stdout .no_fnmatch_line (
201+ "*The above exception was the direct cause of the following exception:*"
202+ )
203+ result .stdout .no_fnmatch_line (
204+ "*During handling of the above exception, another exception occurred:*"
205+ )
206+
180207 def test_noclass (self ) -> None :
181208 with pytest .raises (TypeError ):
182209 pytest .raises ("wrong" , lambda : None ) # type: ignore[call-overload]
You can’t perform that action at this time.
0 commit comments