Enhance errors for exception/warnings matching (#8508)
Co-authored-by: Florian Bruhin <me@the-compiler.org>
This commit is contained in:
committed by
GitHub
parent
3297bb24a9
commit
e9dd3dffab
@@ -420,18 +420,20 @@ def test_match_raises_error(pytester: Pytester) -> None:
|
||||
excinfo.match(r'[123]+')
|
||||
"""
|
||||
)
|
||||
result = pytester.runpytest()
|
||||
result = pytester.runpytest("--tb=short")
|
||||
assert result.ret != 0
|
||||
|
||||
exc_msg = "Regex pattern '[[]123[]]+' does not match 'division by zero'."
|
||||
result.stdout.fnmatch_lines([f"E * AssertionError: {exc_msg}"])
|
||||
match = [
|
||||
r"E .* AssertionError: Regex pattern did not match.",
|
||||
r"E .* Regex: '\[123\]\+'",
|
||||
r"E .* Input: 'division by zero'",
|
||||
]
|
||||
result.stdout.re_match_lines(match)
|
||||
result.stdout.no_fnmatch_line("*__tracebackhide__ = True*")
|
||||
|
||||
result = pytester.runpytest("--fulltrace")
|
||||
assert result.ret != 0
|
||||
result.stdout.fnmatch_lines(
|
||||
["*__tracebackhide__ = True*", f"E * AssertionError: {exc_msg}"]
|
||||
)
|
||||
result.stdout.re_match_lines([r".*__tracebackhide__ = True.*", *match])
|
||||
|
||||
|
||||
class TestFormattedExcinfo:
|
||||
|
||||
@@ -191,10 +191,12 @@ class TestRaises:
|
||||
int("asdf")
|
||||
|
||||
msg = "with base 16"
|
||||
expr = "Regex pattern {!r} does not match \"invalid literal for int() with base 10: 'asdf'\".".format(
|
||||
msg
|
||||
expr = (
|
||||
"Regex pattern did not match.\n"
|
||||
f" Regex: {msg!r}\n"
|
||||
" Input: \"invalid literal for int() with base 10: 'asdf'\""
|
||||
)
|
||||
with pytest.raises(AssertionError, match=re.escape(expr)):
|
||||
with pytest.raises(AssertionError, match="(?m)" + re.escape(expr)):
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
int("asdf", base=10)
|
||||
|
||||
@@ -217,7 +219,7 @@ class TestRaises:
|
||||
with pytest.raises(AssertionError, match="'foo"):
|
||||
raise AssertionError("'bar")
|
||||
(msg,) = excinfo.value.args
|
||||
assert msg == 'Regex pattern "\'foo" does not match "\'bar".'
|
||||
assert msg == '''Regex pattern did not match.\n Regex: "'foo"\n Input: "'bar"'''
|
||||
|
||||
def test_match_failure_exact_string_message(self):
|
||||
message = "Oh here is a message with (42) numbers in parameters"
|
||||
@@ -226,9 +228,10 @@ class TestRaises:
|
||||
raise AssertionError(message)
|
||||
(msg,) = excinfo.value.args
|
||||
assert msg == (
|
||||
"Regex pattern 'Oh here is a message with (42) numbers in "
|
||||
"parameters' does not match 'Oh here is a message with (42) "
|
||||
"numbers in parameters'. Did you mean to `re.escape()` the regex?"
|
||||
"Regex pattern did not match.\n"
|
||||
" Regex: 'Oh here is a message with (42) numbers in parameters'\n"
|
||||
" Input: 'Oh here is a message with (42) numbers in parameters'\n"
|
||||
" Did you mean to `re.escape()` the regex?"
|
||||
)
|
||||
|
||||
def test_raises_match_wrong_type(self):
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import re
|
||||
import warnings
|
||||
from typing import Optional
|
||||
|
||||
@@ -263,7 +262,7 @@ class TestWarns:
|
||||
with pytest.warns(RuntimeWarning):
|
||||
warnings.warn("user", UserWarning)
|
||||
excinfo.match(
|
||||
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted. "
|
||||
r"DID NOT WARN. No warnings of type \(.+RuntimeWarning.+,\) were emitted.\n"
|
||||
r"The list of emitted warnings is: \[UserWarning\('user',?\)\]."
|
||||
)
|
||||
|
||||
@@ -271,15 +270,15 @@ class TestWarns:
|
||||
with pytest.warns(UserWarning):
|
||||
warnings.warn("runtime", RuntimeWarning)
|
||||
excinfo.match(
|
||||
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted. "
|
||||
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)\]."
|
||||
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
|
||||
r"The list of emitted warnings is: \[RuntimeWarning\('runtime',?\)]."
|
||||
)
|
||||
|
||||
with pytest.raises(pytest.fail.Exception) as excinfo:
|
||||
with pytest.warns(UserWarning):
|
||||
pass
|
||||
excinfo.match(
|
||||
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted. "
|
||||
r"DID NOT WARN. No warnings of type \(.+UserWarning.+,\) were emitted.\n"
|
||||
r"The list of emitted warnings is: \[\]."
|
||||
)
|
||||
|
||||
@@ -289,18 +288,14 @@ class TestWarns:
|
||||
warnings.warn("runtime", RuntimeWarning)
|
||||
warnings.warn("import", ImportWarning)
|
||||
|
||||
message_template = (
|
||||
"DID NOT WARN. No warnings of type {0} were emitted. "
|
||||
"The list of emitted warnings is: {1}."
|
||||
)
|
||||
excinfo.match(
|
||||
re.escape(
|
||||
message_template.format(
|
||||
warning_classes, [each.message for each in warninfo]
|
||||
)
|
||||
)
|
||||
messages = [each.message for each in warninfo]
|
||||
expected_str = (
|
||||
f"DID NOT WARN. No warnings of type {warning_classes} were emitted.\n"
|
||||
f"The list of emitted warnings is: {messages}."
|
||||
)
|
||||
|
||||
assert str(excinfo.value) == expected_str
|
||||
|
||||
def test_record(self) -> None:
|
||||
with pytest.warns(UserWarning) as record:
|
||||
warnings.warn("user", UserWarning)
|
||||
|
||||
Reference in New Issue
Block a user