0001 import warnings 0002 import os 0003 import unittest 0004 from test import test_support 0005 0006 # The warnings module isn't easily tested, because it relies on module 0007 # globals to store configuration information. setUp() and tearDown() 0008 # preserve the current settings to avoid bashing them while running tests. 0009 0010 # To capture the warning messages, a replacement for showwarning() is 0011 # used to save warning information in a global variable. 0012 0013 class WarningMessage: 0014 "Holds results of latest showwarning() call" 0015 pass 0016 0017 def showwarning(message, category, filename, lineno, file=None): 0018 msg.message = str(message) 0019 msg.category = category.__name__ 0020 msg.filename = os.path.basename(filename) 0021 msg.lineno = lineno 0022 0023 class TestModule(unittest.TestCase): 0024 0025 def setUp(self): 0026 global msg 0027 msg = WarningMessage() 0028 self._filters = warnings.filters[:] 0029 self._showwarning = warnings.showwarning 0030 warnings.showwarning = showwarning 0031 self.ignored = [w[2].__name__ for w in self._filters 0032 if w[0]=='ignore' and w[1] is None and w[3] is None] 0033 0034 def tearDown(self): 0035 warnings.filters = self._filters[:] 0036 warnings.showwarning = self._showwarning 0037 0038 def test_warn_default_category(self): 0039 for i in range(4): 0040 text = 'multi %d' %i # Different text on each call 0041 warnings.warn(text) 0042 self.assertEqual(msg.message, text) 0043 self.assertEqual(msg.category, 'UserWarning') 0044 0045 def test_warn_specific_category(self): 0046 text = 'None' 0047 # XXX OverflowWarning should go away for Python 2.5. 0048 for category in [DeprecationWarning, FutureWarning, OverflowWarning, 0049 PendingDeprecationWarning, RuntimeWarning, 0050 SyntaxWarning, UserWarning, Warning]: 0051 if category.__name__ in self.ignored: 0052 text = 'filtered out' + category.__name__ 0053 warnings.warn(text, category) 0054 self.assertNotEqual(msg.message, text) 0055 else: 0056 text = 'unfiltered %s' % category.__name__ 0057 warnings.warn(text, category) 0058 self.assertEqual(msg.message, text) 0059 self.assertEqual(msg.category, category.__name__) 0060 0061 def test_filtering(self): 0062 0063 warnings.filterwarnings("error", "", Warning, "", 0) 0064 self.assertRaises(UserWarning, warnings.warn, 'convert to error') 0065 0066 warnings.resetwarnings() 0067 text = 'handle normally' 0068 warnings.warn(text) 0069 self.assertEqual(msg.message, text) 0070 self.assertEqual(msg.category, 'UserWarning') 0071 0072 warnings.filterwarnings("ignore", "", Warning, "", 0) 0073 text = 'filtered out' 0074 warnings.warn(text) 0075 self.assertNotEqual(msg.message, text) 0076 0077 warnings.resetwarnings() 0078 warnings.filterwarnings("error", "hex*", Warning, "", 0) 0079 self.assertRaises(UserWarning, warnings.warn, 'hex/oct') 0080 text = 'nonmatching text' 0081 warnings.warn(text) 0082 self.assertEqual(msg.message, text) 0083 self.assertEqual(msg.category, 'UserWarning') 0084 0085 def test_main(verbose=None): 0086 test_support.run_unittest(TestModule) 0087 0088 if __name__ == "__main__": 0089 test_main(verbose=True) 0090
Generated by PyXR 0.9.4