Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 1 | import warnings |
Raymond Hettinger | dc9dcf1 | 2003-07-13 06:15:11 +0000 | [diff] [blame] | 2 | import os |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 3 | import unittest |
| 4 | from test import test_support |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 5 | |
| 6 | # The warnings module isn't easily tested, because it relies on module |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 7 | # globals to store configuration information. setUp() and tearDown() |
| 8 | # preserve the current settings to avoid bashing them while running tests. |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 9 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 10 | # To capture the warning messages, a replacement for showwarning() is |
| 11 | # used to save warning information in a global variable. |
| 12 | |
| 13 | class WarningMessage: |
| 14 | "Holds results of latest showwarning() call" |
| 15 | pass |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 16 | |
| 17 | def showwarning(message, category, filename, lineno, file=None): |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 18 | msg.message = str(message) |
| 19 | msg.category = category.__name__ |
| 20 | msg.filename = os.path.basename(filename) |
| 21 | msg.lineno = lineno |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 22 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 23 | class TestModule(unittest.TestCase): |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 24 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 25 | def setUp(self): |
| 26 | global msg |
| 27 | msg = WarningMessage() |
| 28 | self._filters = warnings.filters[:] |
| 29 | self._showwarning = warnings.showwarning |
| 30 | warnings.showwarning = showwarning |
| 31 | self.ignored = [w[2].__name__ for w in self._filters |
| 32 | if w[0]=='ignore' and w[1] is None and w[3] is None] |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 33 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 34 | def tearDown(self): |
| 35 | warnings.filters = self._filters[:] |
| 36 | warnings.showwarning = self._showwarning |
Jeremy Hylton | 8501466 | 2003-07-11 15:37:59 +0000 | [diff] [blame] | 37 | |
Raymond Hettinger | d6f6e50 | 2003-07-13 08:37:40 +0000 | [diff] [blame^] | 38 | def test_warn_default_category(self): |
| 39 | for i in range(4): |
| 40 | text = 'multi %d' %i # Different text on each call |
| 41 | warnings.warn(text) |
| 42 | self.assertEqual(msg.message, text) |
| 43 | self.assertEqual(msg.category, 'UserWarning') |
| 44 | |
| 45 | def test_warn_specific_category(self): |
| 46 | text = 'None' |
| 47 | for category in [DeprecationWarning, FutureWarning, OverflowWarning, |
| 48 | PendingDeprecationWarning, RuntimeWarning, |
| 49 | SyntaxWarning, UserWarning, Warning]: |
| 50 | if category.__name__ in self.ignored: |
| 51 | text = 'filtered out' + category.__name__ |
| 52 | warnings.warn(text, category) |
| 53 | self.assertNotEqual(msg.message, text) |
| 54 | else: |
| 55 | text = 'unfiltered %s' % category.__name__ |
| 56 | warnings.warn(text, category) |
| 57 | self.assertEqual(msg.message, text) |
| 58 | self.assertEqual(msg.category, category.__name__) |
| 59 | |
| 60 | def test_filtering(self): |
| 61 | |
| 62 | warnings.filterwarnings("error", "", Warning, "", 0) |
| 63 | self.assertRaises(UserWarning, warnings.warn, 'convert to error') |
| 64 | |
| 65 | warnings.resetwarnings() |
| 66 | text = 'handle normally' |
| 67 | warnings.warn(text) |
| 68 | self.assertEqual(msg.message, text) |
| 69 | self.assertEqual(msg.category, 'UserWarning') |
| 70 | |
| 71 | warnings.filterwarnings("ignore", "", Warning, "", 0) |
| 72 | text = 'filtered out' |
| 73 | warnings.warn(text) |
| 74 | self.assertNotEqual(msg.message, text) |
| 75 | |
| 76 | warnings.resetwarnings() |
| 77 | warnings.filterwarnings("error", "hex*", Warning, "", 0) |
| 78 | self.assertRaises(UserWarning, warnings.warn, 'hex/oct') |
| 79 | text = 'nonmatching text' |
| 80 | warnings.warn(text) |
| 81 | self.assertEqual(msg.message, text) |
| 82 | self.assertEqual(msg.category, 'UserWarning') |
| 83 | |
| 84 | def test_main(verbose=None): |
| 85 | test_support.run_unittest(TestModule) |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | test_main(verbose=True) |