-
Notifications
You must be signed in to change notification settings - Fork 1
/
unitest.py
executable file
·431 lines (390 loc) · 15.4 KB
/
unitest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/python2
# encoding=utf8
# File name: unitest.py
# This file is part of: pyxwf
#
# LICENSE
#
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public license (the "GPL License"), in which case
# the provisions of GPL License are applicable instead of those above.
#
# FEEDBACK & QUESTIONS
#
# For feedback and questions about pyxwf please e-mail one of the
# authors named in the AUTHORS file.
########################################################################
from __future__ import unicode_literals, print_function, division
class Colors(object):
# HEADER = '\033[95m'
Header = '\033[01m'
Warning = '\033[93m'
Success = '\033[01;32m'
Skipped = '\033[94m'
Error = '\033[01;31m'
Failure = Error
UnexpectedSuccess = Warning
ExpectedFailure = Warning
TestClass = Header
TestName = Header
Reset = '\033[0m'
def disable(self):
self.Success = ''
self.Skipped = ''
self.Failure = ''
self.Error = ''
self.UnexpectedSuccess = ''
self.ExpectedFailure = ''
self.TestClass = ''
self.TestName = ''
self.Reset = ''
self.Header = ''
self.Warning = ''
def __call__(self, string, colour):
return "{0}{2}{1}".format(colour, self.Reset, string)
import unittest
from unittest.util import strclass
import os
import sys
import traceback
import argparse
import textwrap
import itertools
import platform
import time
import logging
if platform.python_implementation() == "PyPy":
import numpypy
logging.basicConfig(level=logging.WARNING)
STATE_PASS = 0
STATE_SKIP = 1
STATE_ERROR = 2
STATE_FAILURE = 3
STATE_UNEXPECTED_SUCCESS = 4
STATE_EXPECTED_FAILURE = 5
ttyWidth = 80
haveTTY = os.isatty(sys.stdout.fileno())
if haveTTY:
rows, cols = os.popen('stty size', 'r').read().split()
ttyWidth = int(cols)
epilog = """Please note that for the reason of auto discovery, all modules which match the glob PATTERN will be imported first. For further information see <http://docs.python.org/library/unittest.html#test-discovery>.
This script will return a status code corresponding to the status of the test suite.
0 => All tests successful.
1 => At least one test skipped
2 => At least one test failed expectedly
3 => At least one test succeeded unexpectedly
4 => At least one test failed unexpectedly
5 => At least one test errored
6 => Should never happen. If you encounter this, report a bug.
7 => No tests were found.
"""
epilog = "\n".join(itertools.chain(*(textwrap.wrap(line, width=80) if len(line) > 0 else (line,) for line in epilog.split("\n"))))
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="Run auto-discovered unit tests inside the current directory.",
epilog=epilog
)
parser.add_argument(
"--pattern", "-p",
metavar="PATTERN",
type=unicode,
default="test_*.py",
help="Set a glob pattern for unit-test files. Default is test_*.py",
dest="pattern",
)
parser.add_argument(
"--colors", "-c",
choices=["auto", "on", "off"],
default="auto",
help="Whether to use colors. auto, the default, checks whether stdout is a tty. If so, colors are used.",
dest="colors",
)
parser.add_argument(
"--width", "-w",
default=ttyWidth,
type=int,
help="Terminal width. This is auto-detected on most systems and defaults to 80 if auto-detection fails and no other value is given.",
dest="ttyWidth"
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--strip-module-prefix",
default="test_",
metavar="PREFIX",
type=unicode,
help="Set a prefix which will be stripped from the test module names. This defaults to test_",
dest="stripModulePrefix"
)
group.add_argument(
"--no-strip-module-prefix",
dest="stripModulePrefix",
action="store_const",
const="",
help="Disable stripping of the module name."
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--strip-method-prefix",
default="test_",
metavar="PREFIX",
type=unicode,
help="Set a prefix which will be stripped from the test method names. This defaults to test_",
dest="stripMethodPrefix"
)
group.add_argument(
"--no-strip-method-prefix",
dest="stripMethodPrefix",
action="store_const",
const="",
help="Disable stripping of the module name."
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--no-stats",
dest="noStats",
action="store_true",
default=False,
help="Disable summarizing stats."
)
group.add_argument(
"--force-stats", "-f",
dest="forceStats",
action="store_true",
default=False,
help="Print stats even if quiet is enabled."
)
parser.add_argument(
"--quiet", "-q",
dest="quiet",
action="store_true",
help="Disable stats and informational output; If any test fails, the test, state and traceback is printed to stderr."
)
parser.add_argument(
"--start-at", "-o",
dest="onlySubDir",
type=unicode,
metavar="DIR",
default=os.path.join(os.getcwd(), "tests"),
help="Start test discovery at DIR. Defaults to ./tests"
)
parser.add_argument(
"--project-dir",
dest="projectDir",
type=unicode,
metavar="DIR",
default=os.getcwd(),
help="Set the project directory to DIR; This is important for imports to work correctly. Defaults to . (current directory)"
)
parser.add_argument(
"--stderr",
dest="stderr",
action="store_true",
default=False,
help="Print statistics to stderr instead of stdout; This is useful to separate unittest output from wrapper script output."
)
args = parser.parse_args()
Colors = Colors()
if args.colors == "off" or (args.colors == "auto" and not haveTTY):
Colors.disable()
loader = unittest.TestLoader()
class AwesomeTextResult(unittest.TestResult):
testNoteIndent = " "
stateNames = [
("ok", Colors.Success),
("skipped", Colors.Skipped),
("error", Colors.Error),
("failure", Colors.Failure),
("unexpected success", Colors.UnexpectedSuccess),
("expected failure", Colors.ExpectedFailure)
]
maxStateNameLen = len(stateNames[STATE_UNEXPECTED_SUCCESS][0])
ttyWidth = 80
def __init__(self, ttyWidth, quiet, stripModulePrefix, stripMethodPrefix, *args, **kwargs):
super(AwesomeTextResult, self).__init__(*args, **kwargs)
self._previousPath = None
self.ttyWidth = ttyWidth
self.success = []
self.quiet = quiet
self.stripModulePrefix = stripModulePrefix
self.stripMethodPrefix = stripMethodPrefix
def _strip(self, name, s):
l = len(s)
if l == 0:
return name
if name[:l] == s:
return name[l:]
else:
return name
def _extractModuleAndMethodName(self, test):
try:
methodName = test._testMethodName
except AttributeError:
methodName = str(test).partition(" ")[0]
if methodName == "runTest":
methodName = None
else:
methodName = self._strip(methodName, self.stripMethodPrefix)
modulePath = type(test).__module__.split(".")
modulePath[-1] = self._strip(modulePath[-1], self.stripModulePrefix)
modulePath.append(type(test).__name__)
return (modulePath, methodName)
def _formatTestName(self, name, indent=" "*2, color=Colors.TestName):
testNameLen = self.ttyWidth - (self.maxStateNameLen + (len(indent+" $")) - (len(color) + len(Colors.Reset)))
return ("{1}{0:.<"+unicode(testNameLen)+"s} ").format(Colors(name, color)+" ", indent)
def _printTestName(self, test):
modulePath, methodName = self._extractModuleAndMethodName(test)
if methodName is None:
print(self._formatTestName(".".join(modulePath), "", Colors.TestClass), end='')
self.testNoteIndent = " "*4
else:
if self._previousPath != modulePath:
self._previousPath = modulePath
print("{0}:".format(Colors(".".join(modulePath), Colors.TestClass)))
print(self._formatTestName(methodName), end='')
self.testNoteIndent = " "*6
def _formatPlainFullTestName(self, test):
modulePath, methodName = self._extractModuleAndMethodName(test)
if methodName is None:
methodName = "runTest"
modulePath.append(methodName)
return ".".join(modulePath)
def _printState(self, state):
print(Colors(*self.stateNames[state]))
def _skipBlank(self, s):
for line in s.split("\n"):
line = line.rstrip()
if len(line) > 0:
yield line
def _indented(self, s, indent=None):
indent = indent if indent is not None else self.testNoteIndent
return indent + (("\n"+indent).join(self._skipBlank(s)))
def _formatError(self, err, indent=None):
s = "".join(map(lambda x: x.decode("utf-8"), traceback.format_exception(*err)))
return self._indented(s, indent)
def startTest(self, test):
if not self.quiet:
self._printTestName(test)
super(AwesomeTextResult, self).startTest(test)
def addError(self, test, err):
super(AwesomeTextResult, self).addError(test, err)
if self.quiet:
print("Test case {0} errored:".format(self._formatPlainFullTestName(test)), file=sys.stderr)
print(self._formatError(err, indent=""), file=sys.stderr)
else:
print(Colors("error", Colors.Error))
print(self._formatError(err), file=sys.stderr)
def addFailure(self, test, err):
super(AwesomeTextResult, self).addFailure(test, err)
if self.quiet:
print("Test case {0} failed:".format(self._formatPlainFullTestName(test)), file=sys.stderr)
print(self._formatError(err, indent=""), file=sys.stderr)
else:
print(Colors("failure", Colors.Failure))
print(self._formatError(err), file=sys.stderr)
def addSuccess(self, test):
super(AwesomeTextResult, self).addSuccess(test)
self.success.append(test)
if not self.quiet:
print(Colors("ok", Colors.Success))
def addSkip(self, test, reason):
super(AwesomeTextResult, self).addSkip(test, reason)
if not self.quiet:
print(Colors("skip", Colors.Error))
print(self.testNoteIndent+reason)
else:
print("Test case {0} skipped: {1}".format(self._formatPlainFullTestName(test), reason), file=sys.stderr)
def addExpectedFailure(self, test, err):
super(AwesomeTextResult, self).addExpectedFailure(test, err)
if self.quiet:
print("Test case {0} failed expectedly:".format(self._formatPlainFullTestName(test)), file=sys.stderr)
print(self._formatError(err, indent=""), file=sys.stderr)
else:
print(Colors("expected failure", Colors.ExpectedFailure))
print(self._formatError(err), file=sys.stderr)
def addUnexpectedSuccess(self, test):
super(AwesomeTextResult, self).addUnexpectedSuccess(test)
if self.quiet:
print("Test case {0} succeeded unexpectedly.".format(self._formatPlainFullTestName(test)), file=sys.stderr)
else:
print(Colors("unexpected success", Colors.UnexpectedSuccess))
def _colouredNumber(self, count, nonZero="", zero=""):
return Colors(unicode(count), zero if count == 0 else nonZero)
def getStats(self, suite):
passedCount = len(self.success)
errorCount = len(self.errors)
failureCount = len(self.failures)
skippedCount = len(self.skipped)
expectedFailureCount = len(self.expectedFailures)
unexpectedSuccessCount = len(self.unexpectedSuccesses)
testsTotal = suite.countTestCases()
return passedCount, errorCount, failureCount, skippedCount, expectedFailureCount, unexpectedSuccessCount, testsTotal
def printStats(self, stats, file=sys.stdout):
passedCount, errorCount, failureCount, skippedCount, expectedFailureCount, unexpectedSuccessCount, testsTotal = stats
passedColour = Colors.Warning
if passedCount == self.testsRun:
passedColour = Colors.Success
elif passedCount == 0:
passedColour = Colors.Failure
print("{0} ({1} tests in total):".format(Colors("Statistics", Colors.Header), testsTotal), file=file)
print(" passed : {0}".format(Colors(passedCount, passedColour)), file=file)
print(" skipped : {0}".format(self._colouredNumber(skippedCount, Colors.Skipped, Colors.Success)), file=file)
print(" expected failures : {0}".format(self._colouredNumber(expectedFailureCount, Colors.ExpectedFailure, Colors.Success)), file=file)
print(" unexpected successes : {0}".format(self._colouredNumber(unexpectedSuccessCount, Colors.UnexpectedSuccess, Colors.Success)), file=file)
print(" errors : {0}".format(self._colouredNumber(errorCount, Colors.Error, Colors.Success)), file=file)
print(" failures : {0}".format(self._colouredNumber(failureCount, Colors.Failure, Colors.Success)), file=file)
print(" ran : {0}".format(Colors(self.testsRun, Colors.Success if self.testsRun == testsTotal else Colors.Warning)), file=file)
results = AwesomeTextResult(args.ttyWidth, args.quiet, args.stripModulePrefix, args.stripMethodPrefix)
results.ttyWidth = ttyWidth
tests = loader.discover(args.onlySubDir, args.pattern, args.projectDir)
if tests.countTestCases() == 0:
print("unitest.py: error: no tests found", file=sys.stderr)
sys.exit(7)
if not args.quiet:
print("Running {0} unittests (detected from auto-discovery)".format(tests.countTestCases()))
startTime = time.time()
tests.run(results)
endTime = time.time()
stats = results.getStats(tests)
if args.forceStats or not args.quiet:
if args.stderr:
Colors.disable()
if not args.noStats:
results.printStats(stats, file=(sys.stderr if args.stderr else sys.stdout))
print("Ran {2} tests on {0} in {1} seconds".format(
Colors(platform.python_implementation(), Colors.Header),
Colors("{0:.6f}".format(endTime-startTime), Colors.Header),
Colors(results.testsRun, Colors.Header)
),
file=(sys.stderr if args.stderr else sys.stdout)
)
# determine the exit code
passedCount, errorCount, failureCount, skippedCount, expectedFailureCount, unexpectedSuccessCount, testsTotal = stats
tmpCount = passedCount
if tmpCount == testsTotal:
sys.exit(0)
tmpCount += skippedCount
if tmpCount == testsTotal:
sys.exit(1)
tmpCount += expectedFailureCount
if tmpCount == testsTotal:
sys.exit(2)
tmpCount += unexpectedSuccessCount
if tmpCount == testsTotal:
sys.exit(3)
tmpCount += failureCount
if tmpCount == testsTotal:
sys.exit(4)
tmpCount += errorCount
if tmpCount == testsTotal:
sys.exit(5)
sys.exit(6) # this should never ever happen