annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/unittest/runner.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 """Running tests"""
jpayne@69 2
jpayne@69 3 import sys
jpayne@69 4 import time
jpayne@69 5 import warnings
jpayne@69 6
jpayne@69 7 from . import result
jpayne@69 8 from .signals import registerResult
jpayne@69 9
jpayne@69 10 __unittest = True
jpayne@69 11
jpayne@69 12
jpayne@69 13 class _WritelnDecorator(object):
jpayne@69 14 """Used to decorate file-like objects with a handy 'writeln' method"""
jpayne@69 15 def __init__(self,stream):
jpayne@69 16 self.stream = stream
jpayne@69 17
jpayne@69 18 def __getattr__(self, attr):
jpayne@69 19 if attr in ('stream', '__getstate__'):
jpayne@69 20 raise AttributeError(attr)
jpayne@69 21 return getattr(self.stream,attr)
jpayne@69 22
jpayne@69 23 def writeln(self, arg=None):
jpayne@69 24 if arg:
jpayne@69 25 self.write(arg)
jpayne@69 26 self.write('\n') # text-mode streams translate to \r\n if needed
jpayne@69 27
jpayne@69 28
jpayne@69 29 class TextTestResult(result.TestResult):
jpayne@69 30 """A test result class that can print formatted text results to a stream.
jpayne@69 31
jpayne@69 32 Used by TextTestRunner.
jpayne@69 33 """
jpayne@69 34 separator1 = '=' * 70
jpayne@69 35 separator2 = '-' * 70
jpayne@69 36
jpayne@69 37 def __init__(self, stream, descriptions, verbosity):
jpayne@69 38 super(TextTestResult, self).__init__(stream, descriptions, verbosity)
jpayne@69 39 self.stream = stream
jpayne@69 40 self.showAll = verbosity > 1
jpayne@69 41 self.dots = verbosity == 1
jpayne@69 42 self.descriptions = descriptions
jpayne@69 43
jpayne@69 44 def getDescription(self, test):
jpayne@69 45 doc_first_line = test.shortDescription()
jpayne@69 46 if self.descriptions and doc_first_line:
jpayne@69 47 return '\n'.join((str(test), doc_first_line))
jpayne@69 48 else:
jpayne@69 49 return str(test)
jpayne@69 50
jpayne@69 51 def startTest(self, test):
jpayne@69 52 super(TextTestResult, self).startTest(test)
jpayne@69 53 if self.showAll:
jpayne@69 54 self.stream.write(self.getDescription(test))
jpayne@69 55 self.stream.write(" ... ")
jpayne@69 56 self.stream.flush()
jpayne@69 57
jpayne@69 58 def addSuccess(self, test):
jpayne@69 59 super(TextTestResult, self).addSuccess(test)
jpayne@69 60 if self.showAll:
jpayne@69 61 self.stream.writeln("ok")
jpayne@69 62 elif self.dots:
jpayne@69 63 self.stream.write('.')
jpayne@69 64 self.stream.flush()
jpayne@69 65
jpayne@69 66 def addError(self, test, err):
jpayne@69 67 super(TextTestResult, self).addError(test, err)
jpayne@69 68 if self.showAll:
jpayne@69 69 self.stream.writeln("ERROR")
jpayne@69 70 elif self.dots:
jpayne@69 71 self.stream.write('E')
jpayne@69 72 self.stream.flush()
jpayne@69 73
jpayne@69 74 def addFailure(self, test, err):
jpayne@69 75 super(TextTestResult, self).addFailure(test, err)
jpayne@69 76 if self.showAll:
jpayne@69 77 self.stream.writeln("FAIL")
jpayne@69 78 elif self.dots:
jpayne@69 79 self.stream.write('F')
jpayne@69 80 self.stream.flush()
jpayne@69 81
jpayne@69 82 def addSkip(self, test, reason):
jpayne@69 83 super(TextTestResult, self).addSkip(test, reason)
jpayne@69 84 if self.showAll:
jpayne@69 85 self.stream.writeln("skipped {0!r}".format(reason))
jpayne@69 86 elif self.dots:
jpayne@69 87 self.stream.write("s")
jpayne@69 88 self.stream.flush()
jpayne@69 89
jpayne@69 90 def addExpectedFailure(self, test, err):
jpayne@69 91 super(TextTestResult, self).addExpectedFailure(test, err)
jpayne@69 92 if self.showAll:
jpayne@69 93 self.stream.writeln("expected failure")
jpayne@69 94 elif self.dots:
jpayne@69 95 self.stream.write("x")
jpayne@69 96 self.stream.flush()
jpayne@69 97
jpayne@69 98 def addUnexpectedSuccess(self, test):
jpayne@69 99 super(TextTestResult, self).addUnexpectedSuccess(test)
jpayne@69 100 if self.showAll:
jpayne@69 101 self.stream.writeln("unexpected success")
jpayne@69 102 elif self.dots:
jpayne@69 103 self.stream.write("u")
jpayne@69 104 self.stream.flush()
jpayne@69 105
jpayne@69 106 def printErrors(self):
jpayne@69 107 if self.dots or self.showAll:
jpayne@69 108 self.stream.writeln()
jpayne@69 109 self.printErrorList('ERROR', self.errors)
jpayne@69 110 self.printErrorList('FAIL', self.failures)
jpayne@69 111
jpayne@69 112 def printErrorList(self, flavour, errors):
jpayne@69 113 for test, err in errors:
jpayne@69 114 self.stream.writeln(self.separator1)
jpayne@69 115 self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
jpayne@69 116 self.stream.writeln(self.separator2)
jpayne@69 117 self.stream.writeln("%s" % err)
jpayne@69 118
jpayne@69 119
jpayne@69 120 class TextTestRunner(object):
jpayne@69 121 """A test runner class that displays results in textual form.
jpayne@69 122
jpayne@69 123 It prints out the names of tests as they are run, errors as they
jpayne@69 124 occur, and a summary of the results at the end of the test run.
jpayne@69 125 """
jpayne@69 126 resultclass = TextTestResult
jpayne@69 127
jpayne@69 128 def __init__(self, stream=None, descriptions=True, verbosity=1,
jpayne@69 129 failfast=False, buffer=False, resultclass=None, warnings=None,
jpayne@69 130 *, tb_locals=False):
jpayne@69 131 """Construct a TextTestRunner.
jpayne@69 132
jpayne@69 133 Subclasses should accept **kwargs to ensure compatibility as the
jpayne@69 134 interface changes.
jpayne@69 135 """
jpayne@69 136 if stream is None:
jpayne@69 137 stream = sys.stderr
jpayne@69 138 self.stream = _WritelnDecorator(stream)
jpayne@69 139 self.descriptions = descriptions
jpayne@69 140 self.verbosity = verbosity
jpayne@69 141 self.failfast = failfast
jpayne@69 142 self.buffer = buffer
jpayne@69 143 self.tb_locals = tb_locals
jpayne@69 144 self.warnings = warnings
jpayne@69 145 if resultclass is not None:
jpayne@69 146 self.resultclass = resultclass
jpayne@69 147
jpayne@69 148 def _makeResult(self):
jpayne@69 149 return self.resultclass(self.stream, self.descriptions, self.verbosity)
jpayne@69 150
jpayne@69 151 def run(self, test):
jpayne@69 152 "Run the given test case or test suite."
jpayne@69 153 result = self._makeResult()
jpayne@69 154 registerResult(result)
jpayne@69 155 result.failfast = self.failfast
jpayne@69 156 result.buffer = self.buffer
jpayne@69 157 result.tb_locals = self.tb_locals
jpayne@69 158 with warnings.catch_warnings():
jpayne@69 159 if self.warnings:
jpayne@69 160 # if self.warnings is set, use it to filter all the warnings
jpayne@69 161 warnings.simplefilter(self.warnings)
jpayne@69 162 # if the filter is 'default' or 'always', special-case the
jpayne@69 163 # warnings from the deprecated unittest methods to show them
jpayne@69 164 # no more than once per module, because they can be fairly
jpayne@69 165 # noisy. The -Wd and -Wa flags can be used to bypass this
jpayne@69 166 # only when self.warnings is None.
jpayne@69 167 if self.warnings in ['default', 'always']:
jpayne@69 168 warnings.filterwarnings('module',
jpayne@69 169 category=DeprecationWarning,
jpayne@69 170 message=r'Please use assert\w+ instead.')
jpayne@69 171 startTime = time.perf_counter()
jpayne@69 172 startTestRun = getattr(result, 'startTestRun', None)
jpayne@69 173 if startTestRun is not None:
jpayne@69 174 startTestRun()
jpayne@69 175 try:
jpayne@69 176 test(result)
jpayne@69 177 finally:
jpayne@69 178 stopTestRun = getattr(result, 'stopTestRun', None)
jpayne@69 179 if stopTestRun is not None:
jpayne@69 180 stopTestRun()
jpayne@69 181 stopTime = time.perf_counter()
jpayne@69 182 timeTaken = stopTime - startTime
jpayne@69 183 result.printErrors()
jpayne@69 184 if hasattr(result, 'separator2'):
jpayne@69 185 self.stream.writeln(result.separator2)
jpayne@69 186 run = result.testsRun
jpayne@69 187 self.stream.writeln("Ran %d test%s in %.3fs" %
jpayne@69 188 (run, run != 1 and "s" or "", timeTaken))
jpayne@69 189 self.stream.writeln()
jpayne@69 190
jpayne@69 191 expectedFails = unexpectedSuccesses = skipped = 0
jpayne@69 192 try:
jpayne@69 193 results = map(len, (result.expectedFailures,
jpayne@69 194 result.unexpectedSuccesses,
jpayne@69 195 result.skipped))
jpayne@69 196 except AttributeError:
jpayne@69 197 pass
jpayne@69 198 else:
jpayne@69 199 expectedFails, unexpectedSuccesses, skipped = results
jpayne@69 200
jpayne@69 201 infos = []
jpayne@69 202 if not result.wasSuccessful():
jpayne@69 203 self.stream.write("FAILED")
jpayne@69 204 failed, errored = len(result.failures), len(result.errors)
jpayne@69 205 if failed:
jpayne@69 206 infos.append("failures=%d" % failed)
jpayne@69 207 if errored:
jpayne@69 208 infos.append("errors=%d" % errored)
jpayne@69 209 else:
jpayne@69 210 self.stream.write("OK")
jpayne@69 211 if skipped:
jpayne@69 212 infos.append("skipped=%d" % skipped)
jpayne@69 213 if expectedFails:
jpayne@69 214 infos.append("expected failures=%d" % expectedFails)
jpayne@69 215 if unexpectedSuccesses:
jpayne@69 216 infos.append("unexpected successes=%d" % unexpectedSuccesses)
jpayne@69 217 if infos:
jpayne@69 218 self.stream.writeln(" (%s)" % (", ".join(infos),))
jpayne@69 219 else:
jpayne@69 220 self.stream.write("\n")
jpayne@69 221 return result