summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2007-12-20 10:09:52 (GMT)
committerBrett Cannon <bcannon@gmail.com>2007-12-20 10:09:52 (GMT)
commit905c31c73dd5b00a0fd7f2736b4c273f6e4cfe59 (patch)
tree7f3f556fe59f1d85eda00aa7fa6e5026cee1e783 /Lib/test
parentdff1fd93efb1236437cf1fea33446218d0bc2607 (diff)
downloadcpython-905c31c73dd5b00a0fd7f2736b4c273f6e4cfe59.zip
cpython-905c31c73dd5b00a0fd7f2736b4c273f6e4cfe59.tar.gz
cpython-905c31c73dd5b00a0fd7f2736b4c273f6e4cfe59.tar.bz2
Add tests for the warnings module; specifically formatwarning and showwarning.
Still need tests for warn_explicit and simplefilter.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_warnings.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/Lib/test/test_warnings.py b/Lib/test/test_warnings.py
index 66246cf..417a743 100644
--- a/Lib/test/test_warnings.py
+++ b/Lib/test/test_warnings.py
@@ -1,5 +1,7 @@
import warnings
+import linecache
import os
+import StringIO
import sys
import unittest
from test import test_support
@@ -36,6 +38,8 @@ class TestModule(unittest.TestCase):
self.assert_(w.category is category)
def test_filtering(self):
+ # Test filterwarnings().
+ # Implicitly also tests resetwarnings().
with test_support.catch_warning() as w:
warnings.filterwarnings("error", "", Warning, "", 0)
self.assertRaises(UserWarning, warnings.warn, 'convert to error')
@@ -97,6 +101,33 @@ class TestModule(unittest.TestCase):
self.assertEqual(os.path.basename(w.filename), "sys")
+class WarningsDisplayTests(unittest.TestCase):
+
+ def test_formatwarning(self):
+ message = "msg"
+ category = Warning
+ file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
+ line_num = 3
+ file_line = linecache.getline(file_name, line_num).strip()
+ expect = "%s:%s: %s: %s\n %s\n" % (file_name, line_num, category.__name__,
+ message, file_line)
+ self.failUnlessEqual(warnings.formatwarning(message, category,
+ file_name, line_num),
+ expect)
+
+ def test_showwarning(self):
+ file_name = os.path.splitext(warning_tests.__file__)[0] + '.py'
+ line_num = 3
+ expected_file_line = linecache.getline(file_name, line_num).strip()
+ message = 'msg'
+ category = Warning
+ file_object = StringIO.StringIO()
+ expect = warnings.formatwarning(message, category, file_name, line_num)
+ warnings.showwarning(message, category, file_name, line_num,
+ file_object)
+ self.failUnlessEqual(file_object.getvalue(), expect)
+
+
def test_main(verbose=None):
# Obscure hack so that this test passes after reloads or repeated calls
# to test_main (regrtest -R).
@@ -106,7 +137,7 @@ def test_main(verbose=None):
del warning_tests.__warningregistry__
if hasattr(sys, '__warningregistry__'):
del sys.__warningregistry__
- test_support.run_unittest(TestModule)
+ test_support.run_unittest(TestModule, WarningsDisplayTests)
if __name__ == "__main__":
test_main(verbose=True)