Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 1 | """Running tests""" |
| 2 | |
| 3 | import sys |
| 4 | import time |
| 5 | |
| 6 | from . import result |
| 7 | |
Benjamin Peterson | dccc1fc | 2010-03-22 00:15:53 +0000 | [diff] [blame^] | 8 | __unittest = True |
| 9 | |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 10 | |
| 11 | class _WritelnDecorator(object): |
| 12 | """Used to decorate file-like objects with a handy 'writeln' method""" |
| 13 | def __init__(self,stream): |
| 14 | self.stream = stream |
| 15 | |
| 16 | def __getattr__(self, attr): |
Antoine Pitrou | c63ecee | 2009-11-10 21:34:48 +0000 | [diff] [blame] | 17 | if attr in ('stream', '__getstate__'): |
| 18 | raise AttributeError(attr) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 19 | return getattr(self.stream,attr) |
| 20 | |
| 21 | def writeln(self, arg=None): |
| 22 | if arg: |
| 23 | self.write(arg) |
| 24 | self.write('\n') # text-mode streams translate to \r\n if needed |
| 25 | |
| 26 | |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 27 | class TextTestResult(result.TestResult): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 28 | """A test result class that can print formatted text results to a stream. |
| 29 | |
| 30 | Used by TextTestRunner. |
| 31 | """ |
| 32 | separator1 = '=' * 70 |
| 33 | separator2 = '-' * 70 |
| 34 | |
| 35 | def __init__(self, stream, descriptions, verbosity): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 36 | super(TextTestResult, self).__init__() |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 37 | self.stream = stream |
| 38 | self.showAll = verbosity > 1 |
| 39 | self.dots = verbosity == 1 |
| 40 | self.descriptions = descriptions |
| 41 | |
| 42 | def getDescription(self, test): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 43 | doc_first_line = test.shortDescription() |
| 44 | if self.descriptions and doc_first_line: |
| 45 | return '\n'.join((str(test), doc_first_line)) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 46 | else: |
| 47 | return str(test) |
| 48 | |
| 49 | def startTest(self, test): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 50 | super(TextTestResult, self).startTest(test) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 51 | if self.showAll: |
| 52 | self.stream.write(self.getDescription(test)) |
| 53 | self.stream.write(" ... ") |
| 54 | self.stream.flush() |
| 55 | |
| 56 | def addSuccess(self, test): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 57 | super(TextTestResult, self).addSuccess(test) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 58 | if self.showAll: |
| 59 | self.stream.writeln("ok") |
| 60 | elif self.dots: |
| 61 | self.stream.write('.') |
| 62 | self.stream.flush() |
| 63 | |
| 64 | def addError(self, test, err): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 65 | super(TextTestResult, self).addError(test, err) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 66 | if self.showAll: |
| 67 | self.stream.writeln("ERROR") |
| 68 | elif self.dots: |
| 69 | self.stream.write('E') |
| 70 | self.stream.flush() |
| 71 | |
| 72 | def addFailure(self, test, err): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 73 | super(TextTestResult, self).addFailure(test, err) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 74 | if self.showAll: |
| 75 | self.stream.writeln("FAIL") |
| 76 | elif self.dots: |
| 77 | self.stream.write('F') |
| 78 | self.stream.flush() |
| 79 | |
| 80 | def addSkip(self, test, reason): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 81 | super(TextTestResult, self).addSkip(test, reason) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 82 | if self.showAll: |
| 83 | self.stream.writeln("skipped {0!r}".format(reason)) |
| 84 | elif self.dots: |
| 85 | self.stream.write("s") |
| 86 | self.stream.flush() |
| 87 | |
| 88 | def addExpectedFailure(self, test, err): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 89 | super(TextTestResult, self).addExpectedFailure(test, err) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 90 | if self.showAll: |
| 91 | self.stream.writeln("expected failure") |
| 92 | elif self.dots: |
| 93 | self.stream.write("x") |
| 94 | self.stream.flush() |
| 95 | |
| 96 | def addUnexpectedSuccess(self, test): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 97 | super(TextTestResult, self).addUnexpectedSuccess(test) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 98 | if self.showAll: |
| 99 | self.stream.writeln("unexpected success") |
| 100 | elif self.dots: |
| 101 | self.stream.write("u") |
| 102 | self.stream.flush() |
| 103 | |
| 104 | def printErrors(self): |
| 105 | if self.dots or self.showAll: |
| 106 | self.stream.writeln() |
| 107 | self.printErrorList('ERROR', self.errors) |
| 108 | self.printErrorList('FAIL', self.failures) |
| 109 | |
| 110 | def printErrorList(self, flavour, errors): |
| 111 | for test, err in errors: |
| 112 | self.stream.writeln(self.separator1) |
| 113 | self.stream.writeln("%s: %s" % (flavour,self.getDescription(test))) |
| 114 | self.stream.writeln(self.separator2) |
| 115 | self.stream.writeln("%s" % err) |
| 116 | |
| 117 | |
| 118 | class TextTestRunner(object): |
| 119 | """A test runner class that displays results in textual form. |
| 120 | |
| 121 | It prints out the names of tests as they are run, errors as they |
| 122 | occur, and a summary of the results at the end of the test run. |
| 123 | """ |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 124 | resultclass = TextTestResult |
| 125 | |
| 126 | def __init__(self, stream=sys.stderr, descriptions=True, verbosity=1, |
| 127 | resultclass=None): |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 128 | self.stream = _WritelnDecorator(stream) |
| 129 | self.descriptions = descriptions |
| 130 | self.verbosity = verbosity |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 131 | if resultclass is not None: |
| 132 | self.resultclass = resultclass |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 133 | |
| 134 | def _makeResult(self): |
Michael Foord | 34c9462 | 2010-02-10 15:51:42 +0000 | [diff] [blame] | 135 | return self.resultclass(self.stream, self.descriptions, self.verbosity) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 136 | |
| 137 | def run(self, test): |
| 138 | "Run the given test case or test suite." |
| 139 | result = self._makeResult() |
| 140 | startTime = time.time() |
| 141 | startTestRun = getattr(result, 'startTestRun', None) |
| 142 | if startTestRun is not None: |
| 143 | startTestRun() |
| 144 | try: |
| 145 | test(result) |
| 146 | finally: |
| 147 | stopTestRun = getattr(result, 'stopTestRun', None) |
| 148 | if stopTestRun is not None: |
| 149 | stopTestRun() |
| 150 | stopTime = time.time() |
| 151 | timeTaken = stopTime - startTime |
| 152 | result.printErrors() |
Benjamin Peterson | 847a411 | 2010-03-14 15:04:17 +0000 | [diff] [blame] | 153 | if hasattr(result, 'separator2'): |
| 154 | self.stream.writeln(result.separator2) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 155 | run = result.testsRun |
| 156 | self.stream.writeln("Ran %d test%s in %.3fs" % |
| 157 | (run, run != 1 and "s" or "", timeTaken)) |
| 158 | self.stream.writeln() |
Benjamin Peterson | 847a411 | 2010-03-14 15:04:17 +0000 | [diff] [blame] | 159 | |
| 160 | expectedFails = unexpectedSuccesses = skipped = 0 |
| 161 | try: |
| 162 | results = map(len, (result.expectedFailures, |
| 163 | result.unexpectedSuccesses, |
| 164 | result.skipped)) |
| 165 | expectedFails, unexpectedSuccesses, skipped = results |
| 166 | except AttributeError: |
| 167 | pass |
| 168 | |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 169 | infos = [] |
| 170 | if not result.wasSuccessful(): |
| 171 | self.stream.write("FAILED") |
| 172 | failed, errored = len(result.failures), len(result.errors) |
| 173 | if failed: |
| 174 | infos.append("failures=%d" % failed) |
| 175 | if errored: |
| 176 | infos.append("errors=%d" % errored) |
| 177 | else: |
| 178 | self.stream.write("OK") |
| 179 | if skipped: |
| 180 | infos.append("skipped=%d" % skipped) |
| 181 | if expectedFails: |
| 182 | infos.append("expected failures=%d" % expectedFails) |
| 183 | if unexpectedSuccesses: |
| 184 | infos.append("unexpected successes=%d" % unexpectedSuccesses) |
| 185 | if infos: |
| 186 | self.stream.writeln(" (%s)" % (", ".join(infos),)) |
| 187 | else: |
| 188 | self.stream.write("\n") |
| 189 | return result |