Improvement: extend pytest.raises to support Exception having __repr__ method and initialized with kwargs parameter, except default repr.

This commit is contained in:
bowu 2023-06-03 21:02:06 +08:00
parent 22cfac7b9f
commit 2e72b40739
2 changed files with 5 additions and 2 deletions

View File

@ -709,7 +709,10 @@ class ExceptionInfo(Generic[E]):
__tracebackhide__ = True
value = str(self.value)
if not value:
value = repr(self.value)
repr_value = repr(self.value)
default_repr = f"{self.type.__name__}()"
if repr_value != default_repr:
value = repr_value
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
if regexp == value:
msg += "\n Did you mean to `re.escape()` the regex?"

View File

@ -173,7 +173,7 @@ class TestRaises:
if method == "function":
pytest.raises(ValueError, t)
elif method == "function_match":
pytest.raises(ValueError, t).match(r"ValueError\(\)")
pytest.raises(ValueError, t).match("^$")
else:
with pytest.raises(ValueError):
t()