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/runner.py | |
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/runner.py')
-rw-r--r-- | Lib/unittest/runner.py | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/Lib/unittest/runner.py b/Lib/unittest/runner.py index cd9526d..c5e4258 100644 --- a/Lib/unittest/runner.py +++ b/Lib/unittest/runner.py @@ -2,6 +2,7 @@ import sys import time +import warnings from . import result from .signals import registerResult @@ -125,12 +126,13 @@ class TextTestRunner(object): resultclass = TextTestResult def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, - failfast=False, buffer=False, resultclass=None): + failfast=False, buffer=False, resultclass=None, warnings=None): self.stream = _WritelnDecorator(stream) self.descriptions = descriptions self.verbosity = verbosity self.failfast = failfast self.buffer = buffer + self.warnings = warnings if resultclass is not None: self.resultclass = resultclass @@ -143,17 +145,30 @@ class TextTestRunner(object): registerResult(result) result.failfast = self.failfast result.buffer = self.buffer - startTime = time.time() - startTestRun = getattr(result, 'startTestRun', None) - if startTestRun is not None: - startTestRun() - try: - test(result) - finally: - stopTestRun = getattr(result, 'stopTestRun', None) - if stopTestRun is not None: - stopTestRun() - stopTime = time.time() + with warnings.catch_warnings(): + if self.warnings: + # if self.warnings is set, use it to filter all the warnings + warnings.simplefilter(self.warnings) + # if the filter is 'default' or 'always', special-case the + # warnings from the deprecated unittest methods to show them + # no more than once per module, because they can be fairly + # noisy. The -Wd and -Wa flags can be used to bypass this + # only when self.warnings is None. + if self.warnings in ['default', 'always']: + warnings.filterwarnings('module', + category=DeprecationWarning, + message='Please use assert\w+ instead.') + startTime = time.time() + startTestRun = getattr(result, 'startTestRun', None) + if startTestRun is not None: + startTestRun() + try: + test(result) + finally: + stopTestRun = getattr(result, 'stopTestRun', None) + if stopTestRun is not None: + stopTestRun() + stopTime = time.time() timeTaken = stopTime - startTime result.printErrors() if hasattr(result, 'separator2'): |