def assert_warnings(fn, warning_msgs, regex=False):
"""Assert that each of the given warnings are emitted by fn."""
from .assertions import eq_
with warnings.catch_warnings(record=True) as log:
# ensure that nothing is going into __warningregistry__
warnings.filterwarnings("always")
result = fn()
for warning in log:
popwarn = warning_msgs.pop(0)
if regex:
assert re.match(popwarn, str(warning.message))
else:
eq_(popwarn, str(warning.message))
return result
评论列表
文章目录