diff options
author | Ezio Melotti <ezio.melotti@gmail.com> | 2010-12-01 00:56:10 (GMT) |
---|---|---|
committer | Ezio Melotti <ezio.melotti@gmail.com> | 2010-12-01 00:56:10 (GMT) |
commit | 60901876561a4ef665555e38fae1bdfe7bda78ba (patch) | |
tree | e0f1998dfc9a106dbcef71a614e905bd475dd7db /Lib/unittest/test | |
parent | 00f2f97dbd3db898e5d6c2368edc780a81e2e5fe (diff) | |
download | cpython-60901876561a4ef665555e38fae1bdfe7bda78ba.zip cpython-60901876561a4ef665555e38fae1bdfe7bda78ba.tar.gz cpython-60901876561a4ef665555e38fae1bdfe7bda78ba.tar.bz2 |
#10535: Enable silenced warnings in unittest by default
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/_test_warnings.py | 74 | ||||
-rw-r--r-- | Lib/unittest/test/test_break.py | 6 | ||||
-rw-r--r-- | Lib/unittest/test/test_program.py | 25 | ||||
-rw-r--r-- | Lib/unittest/test/test_runner.py | 58 |
4 files changed, 160 insertions, 3 deletions
diff --git a/Lib/unittest/test/_test_warnings.py b/Lib/unittest/test/_test_warnings.py new file mode 100644 index 0000000..d0be18d --- /dev/null +++ b/Lib/unittest/test/_test_warnings.py @@ -0,0 +1,74 @@ +# helper module for test_runner.Test_TextTestRunner.test_warnings + +""" +This module has a number of tests that raise different kinds of warnings. +When the tests are run, the warnings are caught and their messages are printed +to stdout. This module also accepts an arg that is then passed to +unittest.main to affect the behavior of warnings. +Test_TextTestRunner.test_warnings executes this script with different +combinations of warnings args and -W flags and check that the output is correct. +See #10535. +""" + +import io +import sys +import unittest +import warnings + +def warnfun(): + warnings.warn('rw', RuntimeWarning) + +class TestWarnings(unittest.TestCase): + # unittest warnings will be printed at most once per type (max one message + # for the fail* methods, and one for the assert* methods) + def test_assert(self): + self.assertEquals(2+2, 4) + self.assertEquals(2*2, 4) + self.assertEquals(2**2, 4) + + def test_fail(self): + self.failUnless(1) + self.failUnless(True) + + def test_other_unittest(self): + self.assertAlmostEqual(2+2, 4) + self.assertNotAlmostEqual(4+4, 2) + + # these warnings are normally silenced, but they are printed in unittest + def test_deprecation(self): + warnings.warn('dw', DeprecationWarning) + warnings.warn('dw', DeprecationWarning) + warnings.warn('dw', DeprecationWarning) + + def test_import(self): + warnings.warn('iw', ImportWarning) + warnings.warn('iw', ImportWarning) + warnings.warn('iw', ImportWarning) + + # user warnings should always be printed + def test_warning(self): + warnings.warn('uw') + warnings.warn('uw') + warnings.warn('uw') + + # these warnings come from the same place; they will be printed + # only once by default or three times if the 'always' filter is used + def test_function(self): + + warnfun() + warnfun() + warnfun() + + + +if __name__ == '__main__': + with warnings.catch_warnings(record=True) as ws: + # if an arg is provided pass it to unittest.main as 'warnings' + if len(sys.argv) == 2: + unittest.main(exit=False, warnings=sys.argv.pop()) + else: + unittest.main(exit=False) + + # print all the warning messages collected + for w in ws: + print(w.message) diff --git a/Lib/unittest/test/test_break.py b/Lib/unittest/test/test_break.py index 0e09dfb..77ce201 100644 --- a/Lib/unittest/test/test_break.py +++ b/Lib/unittest/test/test_break.py @@ -209,7 +209,8 @@ class TestBreak(unittest.TestCase): self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None, 'verbosity': verbosity, - 'failfast': failfast})]) + 'failfast': failfast, + 'warnings': None})]) self.assertEqual(FakeRunner.runArgs, [test]) self.assertEqual(p.result, result) @@ -222,7 +223,8 @@ class TestBreak(unittest.TestCase): self.assertEqual(FakeRunner.initArgs, [((), {'buffer': None, 'verbosity': verbosity, - 'failfast': failfast})]) + 'failfast': failfast, + 'warnings': None})]) self.assertEqual(FakeRunner.runArgs, [test]) self.assertEqual(p.result, result) diff --git a/Lib/unittest/test/test_program.py b/Lib/unittest/test/test_program.py index f8e2804..2eb0be3 100644 --- a/Lib/unittest/test/test_program.py +++ b/Lib/unittest/test/test_program.py @@ -182,6 +182,27 @@ class TestCommandLineArgs(unittest.TestCase): program.parseArgs([None, opt]) self.assertEqual(getattr(program, attr), not_none) + def testWarning(self): + """Test the warnings argument""" + # see #10535 + class FakeTP(unittest.TestProgram): + def parseArgs(self, *args, **kw): pass + def runTests(self, *args, **kw): pass + warnoptions = sys.warnoptions + try: + sys.warnoptions[:] = [] + # no warn options, no arg -> default + self.assertEqual(FakeTP().warnings, 'default') + # no warn options, w/ arg -> arg value + self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore') + sys.warnoptions[:] = ['somevalue'] + # warn options, no arg -> None + # warn options, w/ arg -> arg value + self.assertEqual(FakeTP().warnings, None) + self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore') + finally: + sys.warnoptions[:] = warnoptions + def testRunTestsRunnerClass(self): program = self.program @@ -189,12 +210,14 @@ class TestCommandLineArgs(unittest.TestCase): program.verbosity = 'verbosity' program.failfast = 'failfast' program.buffer = 'buffer' + program.warnings = 'warnings' program.runTests() self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity', 'failfast': 'failfast', - 'buffer': 'buffer'}) + 'buffer': 'buffer', + 'warnings': 'warnings'}) self.assertEqual(FakeRunner.test, 'test') self.assertIs(program.result, RESULT) diff --git a/Lib/unittest/test/test_runner.py b/Lib/unittest/test/test_runner.py index 25f7541..efd4962 100644 --- a/Lib/unittest/test/test_runner.py +++ b/Lib/unittest/test/test_runner.py @@ -1,5 +1,8 @@ import io +import os +import sys import pickle +import subprocess import unittest @@ -144,6 +147,7 @@ class Test_TextTestRunner(unittest.TestCase): self.assertFalse(runner.failfast) self.assertFalse(runner.buffer) self.assertEqual(runner.verbosity, 1) + self.assertEqual(runner.warnings, None) self.assertTrue(runner.descriptions) self.assertEqual(runner.resultclass, unittest.TextTestResult) @@ -244,3 +248,57 @@ class Test_TextTestRunner(unittest.TestCase): expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY) self.assertEqual(runner._makeResult(), expectedresult) + + def test_warnings(self): + """ + Check that warnings argument of TextTestRunner correctly affects the + behavior of the warnings. + """ + # see #10535 and the _test_warnings file for more information + + def get_parse_out_err(p): + return [b.splitlines() for b in p.communicate()] + opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE, + cwd=os.path.dirname(__file__)) + ae_msg = b'Please use assertEqual instead.' + at_msg = b'Please use assertTrue instead.' + + # no args -> all the warnings are printed, unittest warnings only once + p = subprocess.Popen([sys.executable, '_test_warnings.py'], **opts) + out, err = get_parse_out_err(p) + self.assertEqual(err[-1], b'OK') + # check that the total number of warnings in the output is correct + self.assertEqual(len(out), 12) + # check that the numbers of the different kind of warnings is correct + for msg in [b'dw', b'iw', b'uw']: + self.assertEqual(out.count(msg), 3) + for msg in [ae_msg, at_msg, b'rw']: + self.assertEqual(out.count(msg), 1) + + args_list = ( + # passing 'ignore' as warnings arg -> no warnings + [sys.executable, '_test_warnings.py', 'ignore'], + # -W doesn't affect the result if the arg is passed + [sys.executable, '-Wa', '_test_warnings.py', 'ignore'], + # -W affects the result if the arg is not passed + [sys.executable, '-Wi', '_test_warnings.py'] + ) + # in all these cases no warnings are printed + for args in args_list: + p = subprocess.Popen(args, **opts) + out, err = get_parse_out_err(p) + self.assertEqual(err[-1], b'OK') + self.assertEqual(len(out), 0) + + + # passing 'always' as warnings arg -> all the warnings printed, + # unittest warnings only once + p = subprocess.Popen([sys.executable, '_test_warnings.py', 'always'], + **opts) + out, err = get_parse_out_err(p) + self.assertEqual(err[-1], b'OK') + self.assertEqual(len(out), 14) + for msg in [b'dw', b'iw', b'uw', b'rw']: + self.assertEqual(out.count(msg), 3) + for msg in [ae_msg, at_msg]: + self.assertEqual(out.count(msg), 1) |