test_warnings: catch stderr and check warning

Check the user warning in test_error_after_default() to not pollute the output,
and check the warning logged into stderr.
This commit is contained in:
Victor Stinner 2016-05-26 15:21:25 +02:00
parent 43593a1892
commit 0025eb145f
1 changed files with 9 additions and 1 deletions

View File

@ -104,7 +104,15 @@ class FilterTests(BaseTest):
message = "FilterTests.test_ignore_after_default" message = "FilterTests.test_ignore_after_default"
def f(): def f():
self.module.warn(message, UserWarning) self.module.warn(message, UserWarning)
f()
with support.captured_stderr() as stderr:
f()
stderr = stderr.getvalue()
self.assertIn("UserWarning: FilterTests.test_ignore_after_default",
stderr)
self.assertIn("self.module.warn(message, UserWarning)",
stderr)
self.module.filterwarnings("error", category=UserWarning) self.module.filterwarnings("error", category=UserWarning)
self.assertRaises(UserWarning, f) self.assertRaises(UserWarning, f)