| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
# TODO: summarize, test harness preconditions (e.g. broker is alive) |
| 21 |
|
| 22 |
import optparse |
| 23 |
import os |
| 24 |
import struct |
| 25 |
import sys |
| 26 |
import time |
| 27 |
import traceback |
| 28 |
import types |
| •29 |
import html |
| 30 |
from fnmatch import fnmatchcase as match |
| 31 |
from logging import getLogger, StreamHandler, Formatter, Filter, \ |
| 32 |
WARN, DEBUG, ERROR, INFO |
| 33 |
|
| 34 |
from .common import SkipTest |
| 35 |
|
| 36 |
if sys.version_info[0] == 3: |
| 37 |
CLASS_TYPES = (type,) |
| 38 |
else: |
| 39 |
CLASS_TYPES = (type, types.ClassType) |
| 40 |
|
| 41 |
levels = { |
| 42 |
"DEBUG": DEBUG, |
| 43 |
"INFO": INFO, |
| 44 |
"WARN": WARN, |
| 45 |
"ERROR": ERROR |
| 46 |
} |
| 47 |
|
| 48 |
sorted_levels = [k for k, v in sorted(levels.items(), key=lambda kv: kv[1])] |
| 49 |
|
| 50 |
parser = optparse.OptionParser(usage="usage: %prog [options] PATTERN ...", |
| 51 |
description="Run tests matching the specified PATTERNs.") |
| 52 |
parser.add_option("-l", "--list", action="store_true", default=False, |
| 53 |
help="list tests instead of executing them") |
| 54 |
parser.add_option("-f", "--log-file", metavar="FILE", help="log output to FILE") |
| 55 |
parser.add_option("-v", "--log-level", metavar="LEVEL", default="WARN", |
| 56 |
help="only display log messages of LEVEL or higher severity: " |
| 57 |
"%s (default %%default)" % ", ".join(sorted_levels)) |
| 58 |
parser.add_option("-c", "--log-category", metavar="CATEGORY", action="append", |
| 59 |
dest="log_categories", default=[], |
| 60 |
help="log only categories matching CATEGORY pattern") |
| 61 |
parser.add_option("-m", "--module", action="append", default=[], |
| 62 |
dest="modules", help="add module to test search path") |
| 63 |
parser.add_option("-i", "--ignore", action="append", default=[], |
| 64 |
help="ignore tests matching IGNORE pattern") |
| 65 |
parser.add_option("-I", "--ignore-file", metavar="IFILE", action="append", |
| 66 |
default=[], |
| 67 |
help="ignore tests matching patterns in IFILE") |
| 68 |
parser.add_option("-H", "--halt-on-error", action="store_true", default=False, |
| 69 |
dest="hoe", help="halt if an error is encountered") |
| 70 |
parser.add_option("-t", "--time", action="store_true", default=False, |
| 71 |
help="report timing information on test run") |
| 72 |
parser.add_option("-D", "--define", metavar="DEFINE", dest="defines", |
| 73 |
action="append", default=[], help="define test parameters") |
| 74 |
parser.add_option("-x", "--xml", metavar="XML", dest="xml", |
| 75 |
help="write test results in Junit style xml suitable for use by CI tools etc") |
| 76 |
parser.add_option("-a", "--always-colorize", action="store_true", dest="always_colorize", default=False, |
| 77 |
help="always colorize the test results rather than relying on terminal tty detection. Useful when invoked from Jython/Maven.") |
| 78 |
parser.add_option("-n", metavar="count", dest="count", type=int, default=1, |
| 79 |
help="run the tests <count> times") |
| 80 |
parser.add_option("-b", "--bare", action="store_true", default=False, |
| 81 |
help="Run bare, i.e. don't capture stack traces. This is useful under Jython as " + |
| 82 |
"captured stack traces do not include the Java portion of the stack," + |
| 83 |
"whereas non captured stack traces do.") |
| 84 |
parser.add_option("-j", "--javatrace", action="store_true", default=False, |
| 85 |
help="Show the full java stack trace. This disables heuristics to eliminate the " + |
| 86 |
"jython portion of java stack traces.") |
| 87 |
|
| 88 |
|
| 89 |
class Config: |
| 90 |
|
| 91 |
def __init__(self): |
| 92 |
self.defines = {} |
| 93 |
self.log_file = None |
| 94 |
self.log_level = WARN |
| 95 |
self.log_categories = [] |
| 96 |
|
| 97 |
|
| 98 |
opts, args = parser.parse_args() |
| 99 |
|
| 100 |
# Known bad tests, skipped unless specifically matched on the command line |
| 101 |
known_bad = ["proton_tests.messenger.*"] |
| 102 |
|
| 103 |
includes = [] |
| 104 |
excludes = ["*__*__"] |
| 105 |
config = Config() |
| 106 |
list_only = opts.list |
| 107 |
for d in opts.defines: |
| 108 |
try: |
| 109 |
idx = d.index("=") |
| 110 |
name = d[:idx] |
| 111 |
value = d[idx + 1:] |
| 112 |
config.defines[name] = value |
| 113 |
except ValueError: |
| 114 |
config.defines[d] = None |
| 115 |
config.log_file = opts.log_file |
| 116 |
config.log_level = levels[opts.log_level.upper()] |
| 117 |
config.log_categories = opts.log_categories |
| 118 |
excludes.extend([v.strip() for v in opts.ignore]) |
| 119 |
for v in opts.ignore_file: |
| 120 |
f = open(v) |
| 121 |
for line in f: |
| 122 |
line = line.strip() |
| 123 |
if line.startswith("#"): |
| 124 |
continue |
| 125 |
excludes.append(line) |
| 126 |
f.close() |
| 127 |
|
| 128 |
for a in args: |
| 129 |
includes.append(a.strip()) |
| 130 |
|
| 131 |
|
| 132 |
def is_ignored(path): |
| 133 |
for p in excludes: |
| 134 |
if match(path, p): |
| 135 |
return True |
| 136 |
return False |
| 137 |
|
| 138 |
|
| 139 |
def is_included(path): |
| 140 |
if is_ignored(path): |
| 141 |
return False |
| 142 |
for p in includes: |
| 143 |
if match(path, p): |
| 144 |
return True |
| 145 |
for p in known_bad: |
| 146 |
if match(path, p): |
| 147 |
return False |
| 148 |
return not includes # If includes is empty, include everything |
| 149 |
|
| 150 |
|
| 151 |
def is_smart(): |
| 152 |
return sys.stdout.isatty() and os.environ.get("TERM", "dumb") != "dumb" |
| 153 |
|
| 154 |
|
| 155 |
try: |
| 156 |
import fcntl |
| 157 |
import termios |
| 158 |
|
| 159 |
def width(): |
| 160 |
if is_smart(): |
| 161 |
s = struct.pack("HHHH", 0, 0, 0, 0) |
| 162 |
fd_stdout = sys.stdout.fileno() |
| 163 |
x = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s) |
| 164 |
rows, cols, xpx, ypx = struct.unpack("HHHH", x) |
| 165 |
return cols |
| 166 |
else: |
| 167 |
try: |
| 168 |
return int(os.environ.get("COLUMNS", "80")) |
| 169 |
except ValueError: |
| 170 |
return 80 |
| 171 |
|
| 172 |
WIDTH = width() |
| 173 |
|
| 174 |
def resize(sig, frm): |
| 175 |
global WIDTH |
| 176 |
WIDTH = width() |
| 177 |
|
| 178 |
import signal |
| 179 |
signal.signal(signal.SIGWINCH, resize) |
| 180 |
|
| 181 |
except ImportError: |
| 182 |
WIDTH = 80 |
| 183 |
|
| 184 |
|
| 185 |
def vt100_attrs(*attrs): |
| 186 |
return "\x1B[%sm" % ";".join(map(str, attrs)) |
| 187 |
|
| 188 |
|
| 189 |
vt100_reset = vt100_attrs(0) |
| 190 |
|
| 191 |
KEYWORDS = {"pass": (32,), |
| 192 |
"skip": (33,), |
| 193 |
"fail": (31,), |
| 194 |
"start": (34,), |
| 195 |
"total": (34,), |
| 196 |
"ignored": (33,), |
| 197 |
"selected": (34,), |
| 198 |
"elapsed": (34,), |
| 199 |
"average": (34,)} |
| 200 |
|
| 201 |
|
| 202 |
def colorize_word(word, text=None): |
| 203 |
if text is None: |
| 204 |
text = word |
| 205 |
return colorize(text, *KEYWORDS.get(word, ())) |
| 206 |
|
| 207 |
|
| 208 |
def colorize(text, *attrs): |
| 209 |
if attrs and (is_smart() or opts.always_colorize): |
| 210 |
return "%s%s%s" % (vt100_attrs(*attrs), text, vt100_reset) |
| 211 |
else: |
| 212 |
return text |
| 213 |
|
| 214 |
|
| 215 |
def indent(text): |
| 216 |
lines = text.split("\n") |
| 217 |
return " %s" % "\n ".join(lines) |
| 218 |
|
| 219 |
# Write a 'minimal' Junit xml style report file suitable for use by CI tools such as Jenkins. |
| 220 |
|
| 221 |
|
| 222 |
class JunitXmlStyleReporter: |
| 223 |
|
| 224 |
def __init__(self, file): |
| 225 |
self.f = open(file, "w") |
| 226 |
|
| 227 |
def begin(self): |
| 228 |
self.f.write('<?xml version="1.0" encoding="UTF-8" ?>\n') |
| 229 |
self.f.write('<testsuite name="pythontests">\n') |
| 230 |
|
| 231 |
def report(self, name, result): |
| 232 |
parts = name.split(".") |
| 233 |
method = parts[-1] |
| 234 |
module = '.'.join(parts[0:-1]) |
| 235 |
self.f.write('<testcase classname="%s" name="%s" time="%f">\n' % (module, method, result.time)) |
| 236 |
if result.failed: |
| •237 |
escaped_type = html.escape(str(result.exception_type)) |
| 238 |
escaped_message = html.escape(str(result.exception_message)) |
| 239 |
self.f.write('<failure type="%s" message="%s">\n' % (escaped_type, escaped_message)) |
| 240 |
self.f.write('<![CDATA[\n') |
| 241 |
self.f.write(result.formatted_exception_trace) |
| 242 |
self.f.write(']]>\n') |
| 243 |
self.f.write('</failure>\n') |
| 244 |
if result.skipped: |
| 245 |
self.f.write('<skipped/>\n') |
| 246 |
self.f.write('</testcase>\n') |
| 247 |
|
| 248 |
def end(self): |
| 249 |
self.f.write('</testsuite>\n') |
| 250 |
self.f.close() |
| 251 |
|
| 252 |
|
| 253 |
class Interceptor: |
| 254 |
|
| 255 |
def __init__(self): |
| 256 |
self.newline = False |
| 257 |
self.indent = False |
| 258 |
self.passthrough = True |
| 259 |
self.dirty = False |
| 260 |
self.last = None |
| 261 |
|
| 262 |
def begin(self): |
| 263 |
self.newline = True |
| 264 |
self.indent = True |
| 265 |
self.passthrough = False |
| 266 |
self.dirty = False |
| 267 |
self.last = None |
| 268 |
|
| 269 |
def reset(self): |
| 270 |
self.newline = False |
| 271 |
self.indent = False |
| 272 |
self.passthrough = True |
| 273 |
|
| 274 |
|
| 275 |
class StreamWrapper: |
| 276 |
|
| 277 |
def __init__(self, interceptor, stream, prefix=" "): |
| 278 |
self.interceptor = interceptor |
| 279 |
self.stream = stream |
| 280 |
self.prefix = prefix |
| 281 |
|
| 282 |
def fileno(self): |
| 283 |
return self.stream.fileno() |
| 284 |
|
| 285 |
def isatty(self): |
| 286 |
return self.stream.isatty() |
| 287 |
|
| 288 |
def write(self, s): |
| 289 |
if self.interceptor.passthrough: |
| 290 |
self.stream.write(s) |
| 291 |
return |
| 292 |
|
| 293 |
if s: |
| 294 |
self.interceptor.dirty = True |
| 295 |
|
| 296 |
if self.interceptor.newline: |
| 297 |
self.interceptor.newline = False |
| 298 |
self.stream.write(" %s\n" % colorize_word("start")) |
| 299 |
self.interceptor.indent = True |
| 300 |
if self.interceptor.indent: |
| 301 |
self.stream.write(self.prefix) |
| 302 |
if s.endswith("\n"): |
| 303 |
s = s.replace("\n", "\n%s" % self.prefix)[:-2] |
| 304 |
self.interceptor.indent = True |
| 305 |
else: |
| 306 |
s = s.replace("\n", "\n%s" % self.prefix) |
| 307 |
self.interceptor.indent = False |
| 308 |
self.stream.write(s) |
| 309 |
|
| 310 |
if s: |
| 311 |
self.interceptor.last = s[-1] |
| 312 |
|
| 313 |
def flush(self): |
| 314 |
self.stream.flush() |
| 315 |
|
| 316 |
|
| 317 |
interceptor = Interceptor() |
| 318 |
|
| 319 |
out_wrp = StreamWrapper(interceptor, sys.stdout) |
| 320 |
err_wrp = StreamWrapper(interceptor, sys.stderr) |
| 321 |
|
| 322 |
out = sys.stdout |
| 323 |
err = sys.stderr |
| 324 |
sys.stdout = out_wrp |
| 325 |
sys.stderr = err_wrp |
| 326 |
|
| 327 |
|
| 328 |
class PatternFilter(Filter): |
| 329 |
|
| 330 |
def __init__(self, *patterns): |
| 331 |
Filter.__init__(self, patterns) |
| 332 |
self.patterns = patterns |
| 333 |
|
| 334 |
def filter(self, record): |
| 335 |
if not self.patterns: |
| 336 |
return True |
| 337 |
for p in self.patterns: |
| 338 |
if match(record.name, p): |
| 339 |
return True |
| 340 |
return False |
| 341 |
|
| 342 |
|
| 343 |
root = getLogger() |
| 344 |
handler = StreamHandler(sys.stdout) |
| 345 |
filter = PatternFilter(*config.log_categories) |
| 346 |
handler.addFilter(filter) |
| 347 |
handler.setFormatter(Formatter("%(asctime)s %(levelname)s %(message)s")) |
| 348 |
root.addHandler(handler) |
| 349 |
root.setLevel(WARN) |
| 350 |
|
| 351 |
log = getLogger("proton.test") |
| 352 |
|
| 353 |
PASS = "pass" |
| 354 |
SKIP = "skip" |
| 355 |
FAIL = "fail" |
| 356 |
|
| 357 |
|
| 358 |
class Runner: |
| 359 |
|
| 360 |
def __init__(self): |
| 361 |
self.exception = None |
| 362 |
self.exception_phase_name = None |
| 363 |
self.skip = False |
| 364 |
|
| 365 |
def passed(self): |
| 366 |
return not self.exception |
| 367 |
|
| 368 |
def skipped(self): |
| 369 |
return self.skip |
| 370 |
|
| 371 |
def failed(self): |
| 372 |
return self.exception and not self.skip |
| 373 |
|
| 374 |
def halt(self): |
| 375 |
"""determines if the overall test execution should be allowed to continue to the next phase""" |
| 376 |
return self.exception or self.skip |
| 377 |
|
| 378 |
def run(self, phase_name, phase): |
| 379 |
"""invokes a test-phase method (which can be the test method itself or a setUp/tearDown |
| 380 |
method). If the method raises an exception the exception is examined to see if the |
| 381 |
exception should be classified as a 'skipped' test""" |
| 382 |
# we don't try to catch exceptions for jython because currently a |
| 383 |
# jython bug will prevent the java portion of the stack being |
| 384 |
# stored with the exception info in the sys module |
| 385 |
if opts.bare: |
| 386 |
phase() |
| 387 |
else: |
| 388 |
try: |
| 389 |
phase() |
| 390 |
except KeyboardInterrupt: |
| 391 |
raise |
| 392 |
except: |
| 393 |
self.exception_phase_name = phase_name |
| 394 |
self.exception = sys.exc_info() |
| 395 |
exception_type = self.exception[0] |
| 396 |
self.skip = getattr(exception_type, "skipped", False) or issubclass(exception_type, SkipTest) |
| 397 |
|
| 398 |
def status(self): |
| 399 |
if self.passed(): |
| 400 |
return PASS |
| 401 |
elif self.skipped(): |
| 402 |
return SKIP |
| 403 |
elif self.failed(): |
| 404 |
return FAIL |
| 405 |
else: |
| 406 |
return None |
| 407 |
|
| 408 |
def get_formatted_exception_trace(self): |
| 409 |
if self.exception: |
| 410 |
if self.skip: |
| 411 |
# format skipped tests without a traceback |
| 412 |
output = indent("".join(traceback.format_exception_only(*self.exception[:2]))).rstrip() |
| 413 |
else: |
| 414 |
output = "Error during %s:" % self.exception_phase_name |
| 415 |
lines = traceback.format_exception(*self.exception) |
| 416 |
val = self.exception[1] |
| 417 |
reflect = False |
| 418 |
if val and hasattr(val, "getStackTrace"): |
| 419 |
jlines = [] |
| 420 |
for frame in val.getStackTrace(): |
| 421 |
if reflect and frame.getClassName().startswith("org.python."): |
| 422 |
# this is a heuristic to eliminate the jython portion of |
| 423 |
# the java stack trace |
| 424 |
if not opts.javatrace: |
| 425 |
break |
| 426 |
jlines.append(" %s\n" % frame.toString()) |
| 427 |
if frame.getClassName().startswith("java.lang.reflect"): |
| 428 |
reflect = True |
| 429 |
else: |
| 430 |
reflect = False |
| 431 |
lines.extend(jlines) |
| 432 |
output += indent("".join(lines)).rstrip() |
| 433 |
return output |
| 434 |
|
| 435 |
def get_exception_type(self): |
| 436 |
if self.exception: |
| 437 |
return self.exception[0] |
| 438 |
else: |
| 439 |
return None |
| 440 |
|
| 441 |
def get_exception_message(self): |
| 442 |
if self.exception: |
| 443 |
return self.exception[1] |
| 444 |
else: |
| 445 |
return None |
| 446 |
|
| 447 |
|
| 448 |
ST_WIDTH = 8 |
| 449 |
|
| 450 |
|
| 451 |
def run_test(name, test, config): |
| 452 |
patterns = filter.patterns |
| 453 |
level = root.level |
| 454 |
filter.patterns = config.log_categories |
| 455 |
root.setLevel(config.log_level) |
| 456 |
|
| 457 |
parts = name.split(".") |
| 458 |
line = None |
| 459 |
output = "" |
| 460 |
for part in parts: |
| 461 |
if line: |
| 462 |
if len(line) + len(part) >= (WIDTH - ST_WIDTH - 1): |
| 463 |
output += "%s. \\\n" % line |
| 464 |
line = " %s" % part |
| 465 |
else: |
| 466 |
line = "%s.%s" % (line, part) |
| 467 |
else: |
| 468 |
line = part |
| 469 |
|
| 470 |
if line: |
| 471 |
output += "%s %s" % (line, (((WIDTH - ST_WIDTH) - len(line)) * ".")) |
| 472 |
sys.stdout.write(output) |
| 473 |
sys.stdout.flush() |
| 474 |
interceptor.begin() |
| 475 |
start = time.time() |
| 476 |
try: |
| 477 |
runner = test() |
| 478 |
finally: |
| 479 |
interceptor.reset() |
| 480 |
end = time.time() |
| 481 |
if interceptor.dirty: |
| 482 |
if interceptor.last != "\n": |
| 483 |
sys.stdout.write("\n") |
| 484 |
sys.stdout.write(output) |
| 485 |
print(" %s" % colorize_word(runner.status())) |
| 486 |
if runner.failed() or runner.skipped(): |
| 487 |
print(runner.get_formatted_exception_trace()) |
| 488 |
root.setLevel(level) |
| 489 |
filter.patterns = patterns |
| 490 |
return TestResult(end - start, |
| 491 |
runner.passed(), |
| 492 |
runner.skipped(), |
| 493 |
runner.failed(), |
| 494 |
runner.get_exception_type(), |
| 495 |
runner.get_exception_message(), |
| 496 |
runner.get_formatted_exception_trace()) |
| 497 |
|
| 498 |
|
| 499 |
class TestResult: |
| 500 |
|
| 501 |
def __init__(self, time, passed, skipped, failed, exception_type, exception_message, formatted_exception_trace): |
| 502 |
self.time = time |
| 503 |
self.passed = passed |
| 504 |
self.skipped = skipped |
| 505 |
self.failed = failed |
| 506 |
self.exception_type = exception_type |
| 507 |
self.exception_message = exception_message |
| 508 |
self.formatted_exception_trace = formatted_exception_trace |
| 509 |
|
| 510 |
|
| 511 |
class FunctionTest: |
| 512 |
|
| 513 |
def __init__(self, test): |
| 514 |
self.test = test |
| 515 |
|
| 516 |
def name(self): |
| 517 |
return "%s.%s" % (self.test.__module__, self.test.__name__) |
| 518 |
|
| 519 |
def run(self): |
| 520 |
return run_test(self.name(), self._run, config) |
| 521 |
|
| 522 |
def _run(self): |
| 523 |
runner = Runner() |
| 524 |
runner.run("test", lambda: self.test(config)) |
| 525 |
return runner |
| 526 |
|
| 527 |
def __repr__(self): |
| 528 |
return "FunctionTest(%r)" % self.test |
| 529 |
|
| 530 |
|
| 531 |
class MethodTest: |
| 532 |
|
| 533 |
def __init__(self, cls, method): |
| 534 |
self.cls = cls |
| 535 |
self.method = method |
| 536 |
|
| 537 |
def name(self): |
| 538 |
return "%s.%s.%s" % (self.cls.__module__, self.cls.__name__, self.method) |
| 539 |
|
| 540 |
def run(self): |
| 541 |
return run_test(self.name(), self._run, config) |
| 542 |
|
| 543 |
def _run(self): |
| 544 |
runner = Runner() |
| 545 |
inst = self.cls(self.method) |
| 546 |
test = getattr(inst, self.method) |
| 547 |
|
| 548 |
if hasattr(inst, "configure"): |
| 549 |
runner.run("configure", lambda: inst.configure(config)) |
| 550 |
if runner.halt(): |
| 551 |
return runner |
| 552 |
if hasattr(inst, "setUp"): |
| 553 |
runner.run("setup", inst.setUp) |
| 554 |
if runner.halt(): |
| 555 |
return runner |
| 556 |
|
| 557 |
runner.run("test", test) |
| 558 |
|
| 559 |
if hasattr(inst, "tearDown"): |
| 560 |
runner.run("teardown", inst.tearDown) |
| 561 |
|
| 562 |
return runner |
| 563 |
|
| 564 |
def __repr__(self): |
| 565 |
return "MethodTest(%r, %r)" % (self.cls, self.method) |
| 566 |
|
| 567 |
|
| 568 |
class PatternMatcher: |
| 569 |
|
| 570 |
def __init__(self, *patterns): |
| 571 |
self.patterns = patterns |
| 572 |
|
| 573 |
def matches(self, name): |
| 574 |
for p in self.patterns: |
| 575 |
if match(name, p): |
| 576 |
return True |
| 577 |
return False |
| 578 |
|
| 579 |
|
| 580 |
class FunctionScanner(PatternMatcher): |
| 581 |
|
| 582 |
def inspect(self, obj): |
| 583 |
return type(obj) == types.FunctionType and self.matches(obj.__name__) |
| 584 |
|
| 585 |
def descend(self, func): |
| 586 |
# the None is required for older versions of python |
| 587 |
return |
| 588 |
yield None |
| 589 |
|
| 590 |
def extract(self, func): |
| 591 |
yield FunctionTest(func) |
| 592 |
|
| 593 |
|
| 594 |
class ClassScanner(PatternMatcher): |
| 595 |
|
| 596 |
def inspect(self, obj): |
| 597 |
return type(obj) in CLASS_TYPES and self.matches(obj.__name__) |
| 598 |
|
| 599 |
def descend(self, cls): |
| 600 |
# the None is required for older versions of python |
| 601 |
return |
| 602 |
yield None |
| 603 |
|
| 604 |
def extract(self, cls): |
| 605 |
for name in sorted(dir(cls)): |
| 606 |
obj = getattr(cls, name) |
| 607 |
if hasattr(obj, '__call__') and name.startswith("test"): |
| 608 |
yield MethodTest(cls, name) |
| 609 |
|
| 610 |
|
| 611 |
class ModuleScanner: |
| 612 |
|
| 613 |
def inspect(self, obj): |
| 614 |
return type(obj) == types.ModuleType |
| 615 |
|
| 616 |
def descend(self, obj): |
| 617 |
for name in sorted(dir(obj)): |
| 618 |
yield getattr(obj, name) |
| 619 |
|
| 620 |
def extract(self, obj): |
| 621 |
# the None is required for older versions of python |
| 622 |
return |
| 623 |
yield None |
| 624 |
|
| 625 |
|
| 626 |
class Harness: |
| 627 |
|
| 628 |
def __init__(self): |
| 629 |
self.scanners = [ |
| 630 |
ModuleScanner(), |
| 631 |
ClassScanner("*Test", "*Tests", "*TestCase"), |
| 632 |
FunctionScanner("test_*") |
| 633 |
] |
| 634 |
self.tests = [] |
| 635 |
self.scanned = [] |
| 636 |
|
| 637 |
def scan(self, *roots): |
| 638 |
objects = list(roots) |
| 639 |
|
| 640 |
while objects: |
| 641 |
obj = objects.pop(0) |
| 642 |
for s in self.scanners: |
| 643 |
if s.inspect(obj): |
| 644 |
self.tests.extend(s.extract(obj)) |
| 645 |
for child in s.descend(obj): |
| 646 |
if not (child in self.scanned or child in objects): |
| 647 |
objects.append(child) |
| 648 |
self.scanned.append(obj) |
| 649 |
|
| 650 |
|
| 651 |
modules = opts.modules |
| 652 |
if not modules: |
| 653 |
modules.extend(["proton_tests"]) |
| 654 |
h = Harness() |
| 655 |
for name in modules: |
| 656 |
m = __import__(name, None, None, ["dummy"]) |
| 657 |
h.scan(m) |
| 658 |
|
| 659 |
filtered = [t for t in h.tests if is_included(t.name())] |
| 660 |
ignored = [t for t in h.tests if is_ignored(t.name())] |
| 661 |
total = len(filtered) + len(ignored) |
| 662 |
|
| 663 |
if opts.xml and not list_only: |
| 664 |
xmlr = JunitXmlStyleReporter(opts.xml) |
| 665 |
xmlr.begin() |
| 666 |
else: |
| 667 |
xmlr = None |
| 668 |
|
| 669 |
|
| 670 |
def runthrough(): |
| 671 |
passed = 0 |
| 672 |
failed = 0 |
| 673 |
skipped = 0 |
| 674 |
start = time.time() |
| 675 |
for t in filtered: |
| 676 |
if list_only: |
| 677 |
print(t.name()) |
| 678 |
else: |
| 679 |
st = t.run() |
| 680 |
if xmlr: |
| 681 |
xmlr.report(t.name(), st) |
| 682 |
if st.passed: |
| 683 |
passed += 1 |
| 684 |
elif st.skipped: |
| 685 |
skipped += 1 |
| 686 |
elif st.failed: |
| 687 |
failed += 1 |
| 688 |
if opts.hoe: |
| 689 |
break |
| 690 |
end = time.time() |
| 691 |
|
| 692 |
run = passed + failed |
| 693 |
|
| 694 |
if not list_only: |
| 695 |
if passed: |
| 696 |
_pass = "pass" |
| 697 |
else: |
| 698 |
_pass = "fail" |
| 699 |
if failed: |
| 700 |
outcome = "fail" |
| 701 |
else: |
| 702 |
outcome = "pass" |
| 703 |
if ignored: |
| 704 |
ign = "ignored" |
| 705 |
else: |
| 706 |
ign = "pass" |
| 707 |
if skipped: |
| 708 |
skip = "skip" |
| 709 |
else: |
| 710 |
skip = "pass" |
| 711 |
sys.stdout.write(colorize("Totals: ", 1)) |
| 712 |
totals = [colorize_word("total", "%s tests" % total), |
| 713 |
colorize_word(_pass, "%s passed" % passed), |
| 714 |
colorize_word(skip, "%s skipped" % skipped), |
| 715 |
colorize_word(ign, "%s ignored" % len(ignored)), |
| 716 |
colorize_word(outcome, "%s failed" % failed)] |
| 717 |
sys.stdout.write(", ".join(totals)) |
| 718 |
if opts.hoe and failed > 0: |
| 719 |
print(" -- (halted after %s)" % run) |
| 720 |
else: |
| 721 |
print("") |
| 722 |
if opts.time and run > 0: |
| 723 |
sys.stdout.write(colorize("Timing:", 1)) |
| 724 |
timing = [colorize_word("elapsed", "%.2fs elapsed" % (end - start)), |
| 725 |
colorize_word("average", "%.2fs average" % ((end - start) / run))] |
| 726 |
print(", ".join(timing)) |
| 727 |
|
| 728 |
if xmlr: |
| 729 |
xmlr.end() |
| 730 |
|
| 731 |
return failed |
| 732 |
|
| 733 |
|
| 734 |
def main(): |
| 735 |
limit = opts.count |
| 736 |
count = 0 |
| 737 |
failures = False |
| 738 |
while limit == 0 or count < limit: |
| 739 |
count += 1 |
| 740 |
if runthrough(): |
| 741 |
failures = True |
| 742 |
if count > 1: |
| 743 |
print(" -- (failures after %s runthroughs)" % count) |
| 744 |
else: |
| 745 |
continue |
| 746 |
|
| 747 |
if failures: |
| 748 |
return(1) |
| 749 |
else: |
| 750 |
return(0) |