annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/unittest/runner.py @ 68:5028fdace37b

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