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