|
1 | 1 | import io |
| 2 | +import os |
| 3 | +import sys |
2 | 4 | import pickle |
| 5 | +import subprocess |
3 | 6 |
|
4 | 7 | import unittest |
5 | 8 |
|
@@ -144,6 +147,7 @@ def test_init(self): |
144 | 147 | self.assertFalse(runner.failfast) |
145 | 148 | self.assertFalse(runner.buffer) |
146 | 149 | self.assertEqual(runner.verbosity, 1) |
| 150 | + self.assertEqual(runner.warnings, None) |
147 | 151 | self.assertTrue(runner.descriptions) |
148 | 152 | self.assertEqual(runner.resultclass, unittest.TextTestResult) |
149 | 153 |
|
@@ -244,3 +248,57 @@ def MockResultClass(*args): |
244 | 248 |
|
245 | 249 | expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY) |
246 | 250 | self.assertEqual(runner._makeResult(), expectedresult) |
| 251 | + |
| 252 | + def test_warnings(self): |
| 253 | + """ |
| 254 | + Check that warnings argument of TextTestRunner correctly affects the |
| 255 | + behavior of the warnings. |
| 256 | + """ |
| 257 | + # see #10535 and the _test_warnings file for more information |
| 258 | + |
| 259 | + def get_parse_out_err(p): |
| 260 | + return [b.splitlines() for b in p.communicate()] |
| 261 | + opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 262 | + cwd=os.path.dirname(__file__)) |
| 263 | + ae_msg = b'Please use assertEqual instead.' |
| 264 | + at_msg = b'Please use assertTrue instead.' |
| 265 | + |
| 266 | + # no args -> all the warnings are printed, unittest warnings only once |
| 267 | + p = subprocess.Popen([sys.executable, '_test_warnings.py'], **opts) |
| 268 | + out, err = get_parse_out_err(p) |
| 269 | + self.assertEqual(err[-1], b'OK') |
| 270 | + # check that the total number of warnings in the output is correct |
| 271 | + self.assertEqual(len(out), 12) |
| 272 | + # check that the numbers of the different kind of warnings is correct |
| 273 | + for msg in [b'dw', b'iw', b'uw']: |
| 274 | + self.assertEqual(out.count(msg), 3) |
| 275 | + for msg in [ae_msg, at_msg, b'rw']: |
| 276 | + self.assertEqual(out.count(msg), 1) |
| 277 | + |
| 278 | + args_list = ( |
| 279 | + # passing 'ignore' as warnings arg -> no warnings |
| 280 | + [sys.executable, '_test_warnings.py', 'ignore'], |
| 281 | + # -W doesn't affect the result if the arg is passed |
| 282 | + [sys.executable, '-Wa', '_test_warnings.py', 'ignore'], |
| 283 | + # -W affects the result if the arg is not passed |
| 284 | + [sys.executable, '-Wi', '_test_warnings.py'] |
| 285 | + ) |
| 286 | + # in all these cases no warnings are printed |
| 287 | + for args in args_list: |
| 288 | + p = subprocess.Popen(args, **opts) |
| 289 | + out, err = get_parse_out_err(p) |
| 290 | + self.assertEqual(err[-1], b'OK') |
| 291 | + self.assertEqual(len(out), 0) |
| 292 | + |
| 293 | + |
| 294 | + # passing 'always' as warnings arg -> all the warnings printed, |
| 295 | + # unittest warnings only once |
| 296 | + p = subprocess.Popen([sys.executable, '_test_warnings.py', 'always'], |
| 297 | + **opts) |
| 298 | + out, err = get_parse_out_err(p) |
| 299 | + self.assertEqual(err[-1], b'OK') |
| 300 | + self.assertEqual(len(out), 14) |
| 301 | + for msg in [b'dw', b'iw', b'uw', b'rw']: |
| 302 | + self.assertEqual(out.count(msg), 3) |
| 303 | + for msg in [ae_msg, at_msg]: |
| 304 | + self.assertEqual(out.count(msg), 1) |
0 commit comments