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 __tracebackhide__ = True
value = str(self.value) value = str(self.value)
if not 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}" msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
if regexp == value: if regexp == value:
msg += "\n Did you mean to `re.escape()` the regex?" msg += "\n Did you mean to `re.escape()` the regex?"

View File

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