ยปCore Development>Code coverage>Lib/unittest/test/_test_warnings.py

Python code coverage for Lib/unittest/test/_test_warnings.py

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