1 | n/a | # helper module for test_runner.Test_TextTestRunner.test_warnings |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | This module has a number of tests that raise different kinds of warnings. |
---|
5 | n/a | When the tests are run, the warnings are caught and their messages are printed |
---|
6 | n/a | to stdout. This module also accepts an arg that is then passed to |
---|
7 | n/a | unittest.main to affect the behavior of warnings. |
---|
8 | n/a | Test_TextTestRunner.test_warnings executes this script with different |
---|
9 | n/a | combinations of warnings args and -W flags and check that the output is correct. |
---|
10 | n/a | See #10535. |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | import sys |
---|
14 | n/a | import unittest |
---|
15 | n/a | import warnings |
---|
16 | n/a | |
---|
17 | n/a | def warnfun(): |
---|
18 | n/a | warnings.warn('rw', RuntimeWarning) |
---|
19 | n/a | |
---|
20 | n/a | class TestWarnings(unittest.TestCase): |
---|
21 | n/a | # unittest warnings will be printed at most once per type (max one message |
---|
22 | n/a | # for the fail* methods, and one for the assert* methods) |
---|
23 | n/a | def test_assert(self): |
---|
24 | n/a | self.assertEquals(2+2, 4) |
---|
25 | n/a | self.assertEquals(2*2, 4) |
---|
26 | n/a | self.assertEquals(2**2, 4) |
---|
27 | n/a | |
---|
28 | n/a | def test_fail(self): |
---|
29 | n/a | self.failUnless(1) |
---|
30 | n/a | self.failUnless(True) |
---|
31 | n/a | |
---|
32 | n/a | def test_other_unittest(self): |
---|
33 | n/a | self.assertAlmostEqual(2+2, 4) |
---|
34 | n/a | self.assertNotAlmostEqual(4+4, 2) |
---|
35 | n/a | |
---|
36 | n/a | # these warnings are normally silenced, but they are printed in unittest |
---|
37 | n/a | def test_deprecation(self): |
---|
38 | n/a | warnings.warn('dw', DeprecationWarning) |
---|
39 | n/a | warnings.warn('dw', DeprecationWarning) |
---|
40 | n/a | warnings.warn('dw', DeprecationWarning) |
---|
41 | n/a | |
---|
42 | n/a | def test_import(self): |
---|
43 | n/a | warnings.warn('iw', ImportWarning) |
---|
44 | n/a | warnings.warn('iw', ImportWarning) |
---|
45 | n/a | warnings.warn('iw', ImportWarning) |
---|
46 | n/a | |
---|
47 | n/a | # user warnings should always be printed |
---|
48 | n/a | def test_warning(self): |
---|
49 | n/a | warnings.warn('uw') |
---|
50 | n/a | warnings.warn('uw') |
---|
51 | n/a | warnings.warn('uw') |
---|
52 | n/a | |
---|
53 | n/a | # these warnings come from the same place; they will be printed |
---|
54 | n/a | # only once by default or three times if the 'always' filter is used |
---|
55 | n/a | def test_function(self): |
---|
56 | n/a | |
---|
57 | n/a | warnfun() |
---|
58 | n/a | warnfun() |
---|
59 | n/a | warnfun() |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | |
---|
63 | n/a | if __name__ == '__main__': |
---|
64 | n/a | with warnings.catch_warnings(record=True) as ws: |
---|
65 | n/a | # if an arg is provided pass it to unittest.main as 'warnings' |
---|
66 | n/a | if len(sys.argv) == 2: |
---|
67 | n/a | unittest.main(exit=False, warnings=sys.argv.pop()) |
---|
68 | n/a | else: |
---|
69 | n/a | unittest.main(exit=False) |
---|
70 | n/a | |
---|
71 | n/a | # print all the warning messages collected |
---|
72 | n/a | for w in ws: |
---|
73 | n/a | print(w.message) |
---|