| 1 |
""" terminal reporting of the full testing process. |
| 2 |
|
| 3 |
This is a good source for looking at the various reporting hooks. |
| 4 |
""" |
| 5 |
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \ |
| 6 |
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED |
| 7 |
import pytest |
| 8 |
import py |
| 9 |
import sys |
| 10 |
import time |
| 11 |
import platform |
| 12 |
|
| 13 |
import _pytest._pluggy as pluggy |
| 14 |
|
| 15 |
|
| 16 |
def pytest_addoption(parser): |
| 17 |
group = parser.getgroup("terminal reporting", "reporting", after="general") |
| 18 |
group._addoption('-v', '--verbose', action="count", |
| 19 |
dest="verbose", default=0, help="increase verbosity."), |
| 20 |
group._addoption('-q', '--quiet', action="count", |
| 21 |
dest="quiet", default=0, help="decrease verbosity."), |
| 22 |
group._addoption('-r', |
| 23 |
action="store", dest="reportchars", default=None, metavar="chars", |
| 24 |
help="show extra test summary info as specified by chars (f)ailed, " |
| 25 |
"(E)error, (s)skipped, (x)failed, (X)passed (w)pytest-warnings " |
| 26 |
"(p)passed, (P)passed with output, (a)all except pP.") |
| 27 |
group._addoption('-l', '--showlocals', |
| 28 |
action="store_true", dest="showlocals", default=False, |
| 29 |
help="show locals in tracebacks (disabled by default).") |
| 30 |
group._addoption('--report', |
| 31 |
action="store", dest="report", default=None, metavar="opts", |
| 32 |
help="(deprecated, use -r)") |
| 33 |
group._addoption('--tb', metavar="style", |
| 34 |
action="store", dest="tbstyle", default='auto', |
| 35 |
choices=['auto', 'long', 'short', 'no', 'line', 'native'], |
| 36 |
help="traceback print mode (auto/long/short/line/native/no).") |
| 37 |
group._addoption('--fulltrace', '--full-trace', |
| 38 |
action="store_true", default=False, |
| 39 |
help="don't cut any tracebacks (default is to cut).") |
| 40 |
group._addoption('--color', metavar="color", |
| 41 |
action="store", dest="color", default='auto', |
| 42 |
choices=['yes', 'no', 'auto'], |
| 43 |
help="color terminal output (yes/no/auto).") |
| 44 |
|
| 45 |
def pytest_configure(config): |
| 46 |
config.option.verbose -= config.option.quiet |
| 47 |
reporter = TerminalReporter(config, sys.stdout) |
| 48 |
config.pluginmanager.register(reporter, 'terminalreporter') |
| 49 |
if config.option.debug or config.option.traceconfig: |
| 50 |
def mywriter(tags, args): |
| 51 |
msg = " ".join(map(str, args)) |
| 52 |
reporter.write_line("[traceconfig] " + msg) |
| 53 |
config.trace.root.setprocessor("pytest:config", mywriter) |
| 54 |
|
| 55 |
def getreportopt(config): |
| 56 |
reportopts = "" |
| 57 |
optvalue = config.option.report |
| 58 |
if optvalue: |
| 59 |
py.builtin.print_("DEPRECATED: use -r instead of --report option.", |
| 60 |
file=sys.stderr) |
| 61 |
if optvalue: |
| 62 |
for setting in optvalue.split(","): |
| 63 |
setting = setting.strip() |
| 64 |
if setting == "skipped": |
| 65 |
reportopts += "s" |
| 66 |
elif setting == "xfailed": |
| 67 |
reportopts += "x" |
| 68 |
reportchars = config.option.reportchars |
| 69 |
if reportchars: |
| 70 |
for char in reportchars: |
| 71 |
if char not in reportopts and char != 'a': |
| 72 |
reportopts += char |
| 73 |
elif char == 'a': |
| 74 |
reportopts = 'fEsxXw' |
| 75 |
return reportopts |
| 76 |
|
| 77 |
def pytest_report_teststatus(report): |
| 78 |
if report.passed: |
| 79 |
letter = "." |
| 80 |
elif report.skipped: |
| 81 |
letter = "s" |
| 82 |
elif report.failed: |
| 83 |
letter = "F" |
| 84 |
if report.when != "call": |
| 85 |
letter = "f" |
| 86 |
return report.outcome, letter, report.outcome.upper() |
| 87 |
|
| 88 |
class WarningReport: |
| 89 |
def __init__(self, code, message, nodeid=None, fslocation=None): |
| 90 |
self.code = code |
| 91 |
self.message = message |
| 92 |
self.nodeid = nodeid |
| 93 |
self.fslocation = fslocation |
| 94 |
|
| 95 |
|
| 96 |
class TerminalReporter: |
| 97 |
def __init__(self, config, file=None): |
| 98 |
import _pytest.config |
| 99 |
self.config = config |
| 100 |
self.verbosity = self.config.option.verbose |
| 101 |
self.showheader = self.verbosity >= 0 |
| 102 |
self.showfspath = self.verbosity >= 0 |
| 103 |
self.showlongtestinfo = self.verbosity > 0 |
| 104 |
self._numcollected = 0 |
| 105 |
|
| 106 |
self.stats = {} |
| 107 |
self.startdir = py.path.local() |
| 108 |
if file is None: |
| 109 |
file = sys.stdout |
| 110 |
self._tw = self.writer = _pytest.config.create_terminal_writer(config, |
| 111 |
file) |
| 112 |
self.currentfspath = None |
| 113 |
self.reportchars = getreportopt(config) |
| 114 |
self.hasmarkup = self._tw.hasmarkup |
| 115 |
self.isatty = file.isatty() |
| 116 |
|
| 117 |
def hasopt(self, char): |
| 118 |
char = {'xfailed': 'x', 'skipped': 's'}.get(char, char) |
| 119 |
return char in self.reportchars |
| 120 |
|
| 121 |
def write_fspath_result(self, nodeid, res): |
| 122 |
fspath = self.config.rootdir.join(nodeid.split("::")[0]) |
| 123 |
if fspath != self.currentfspath: |
| 124 |
self.currentfspath = fspath |
| 125 |
fspath = self.startdir.bestrelpath(fspath) |
| 126 |
self._tw.line() |
| 127 |
self._tw.write(fspath + " ") |
| 128 |
self._tw.write(res) |
| 129 |
|
| 130 |
def write_ensure_prefix(self, prefix, extra="", **kwargs): |
| 131 |
if self.currentfspath != prefix: |
| 132 |
self._tw.line() |
| 133 |
self.currentfspath = prefix |
| 134 |
self._tw.write(prefix) |
| 135 |
if extra: |
| 136 |
self._tw.write(extra, **kwargs) |
| 137 |
self.currentfspath = -2 |
| 138 |
|
| 139 |
def ensure_newline(self): |
| 140 |
if self.currentfspath: |
| 141 |
self._tw.line() |
| 142 |
self.currentfspath = None |
| 143 |
|
| 144 |
def write(self, content, **markup): |
| 145 |
self._tw.write(content, **markup) |
| 146 |
|
| 147 |
def write_line(self, line, **markup): |
| 148 |
if not py.builtin._istext(line): |
| 149 |
line = py.builtin.text(line, errors="replace") |
| 150 |
self.ensure_newline() |
| 151 |
self._tw.line(line, **markup) |
| 152 |
|
| 153 |
def rewrite(self, line, **markup): |
| 154 |
line = str(line) |
| 155 |
self._tw.write("\r" + line, **markup) |
| 156 |
|
| 157 |
def write_sep(self, sep, title=None, **markup): |
| 158 |
self.ensure_newline() |
| 159 |
self._tw.sep(sep, title, **markup) |
| 160 |
|
| 161 |
def section(self, title, sep="=", **kw): |
| 162 |
self._tw.sep(sep, title, **kw) |
| 163 |
|
| 164 |
def line(self, msg, **kw): |
| 165 |
self._tw.line(msg, **kw) |
| 166 |
|
| 167 |
def pytest_internalerror(self, excrepr): |
| 168 |
for line in py.builtin.text(excrepr).split("\n"): |
| 169 |
self.write_line("INTERNALERROR> " + line) |
| 170 |
return 1 |
| 171 |
|
| 172 |
def pytest_logwarning(self, code, fslocation, message, nodeid): |
| 173 |
warnings = self.stats.setdefault("warnings", []) |
| 174 |
if isinstance(fslocation, tuple): |
| 175 |
fslocation = "%s:%d" % fslocation |
| 176 |
warning = WarningReport(code=code, fslocation=fslocation, |
| 177 |
message=message, nodeid=nodeid) |
| 178 |
warnings.append(warning) |
| 179 |
|
| 180 |
def pytest_plugin_registered(self, plugin): |
| 181 |
if self.config.option.traceconfig: |
| 182 |
msg = "PLUGIN registered: %s" % (plugin,) |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
self.write_line(msg) |
| 187 |
|
| 188 |
def pytest_deselected(self, items): |
| 189 |
self.stats.setdefault('deselected', []).extend(items) |
| 190 |
|
| 191 |
def pytest_runtest_logstart(self, nodeid, location): |
| 192 |
|
| 193 |
|
| 194 |
if self.showlongtestinfo: |
| 195 |
line = self._locationline(nodeid, *location) |
| 196 |
self.write_ensure_prefix(line, "") |
| 197 |
elif self.showfspath: |
| 198 |
fsid = nodeid.split("::")[0] |
| 199 |
self.write_fspath_result(fsid, "") |
| 200 |
|
| 201 |
def pytest_runtest_logreport(self, report): |
| 202 |
rep = report |
| 203 |
res = self.config.hook.pytest_report_teststatus(report=rep) |
| 204 |
cat, letter, word = res |
| 205 |
self.stats.setdefault(cat, []).append(rep) |
| 206 |
self._tests_ran = True |
| 207 |
if not letter and not word: |
| 208 |
|
| 209 |
return |
| 210 |
if self.verbosity <= 0: |
| 211 |
if not hasattr(rep, 'node') and self.showfspath: |
| 212 |
self.write_fspath_result(rep.nodeid, letter) |
| 213 |
else: |
| 214 |
self._tw.write(letter) |
| 215 |
else: |
| 216 |
if isinstance(word, tuple): |
| 217 |
word, markup = word |
| 218 |
else: |
| 219 |
if rep.passed: |
| 220 |
markup = {'green':True} |
| 221 |
elif rep.failed: |
| 222 |
markup = {'red':True} |
| 223 |
elif rep.skipped: |
| 224 |
markup = {'yellow':True} |
| 225 |
line = self._locationline(rep.nodeid, *rep.location) |
| 226 |
if not hasattr(rep, 'node'): |
| 227 |
self.write_ensure_prefix(line, word, **markup) |
| 228 |
|
| 229 |
else: |
| 230 |
self.ensure_newline() |
| 231 |
if hasattr(rep, 'node'): |
| 232 |
self._tw.write("[%s] " % rep.node.gateway.id) |
| 233 |
self._tw.write(word, **markup) |
| 234 |
self._tw.write(" " + line) |
| 235 |
self.currentfspath = -2 |
| 236 |
|
| 237 |
def pytest_collection(self): |
| 238 |
if not self.isatty and self.config.option.verbose >= 1: |
| 239 |
self.write("collecting ... ", bold=True) |
| 240 |
|
| 241 |
def pytest_collectreport(self, report): |
| 242 |
if report.failed: |
| 243 |
self.stats.setdefault("error", []).append(report) |
| 244 |
elif report.skipped: |
| 245 |
self.stats.setdefault("skipped", []).append(report) |
| 246 |
items = [x for x in report.result if isinstance(x, pytest.Item)] |
| 247 |
self._numcollected += len(items) |
| 248 |
if self.isatty: |
| 249 |
|
| 250 |
self.report_collect() |
| 251 |
|
| 252 |
def report_collect(self, final=False): |
| 253 |
if self.config.option.verbose < 0: |
| 254 |
return |
| 255 |
|
| 256 |
errors = len(self.stats.get('error', [])) |
| 257 |
skipped = len(self.stats.get('skipped', [])) |
| 258 |
if final: |
| 259 |
line = "collected " |
| 260 |
else: |
| 261 |
line = "collecting " |
| 262 |
line += str(self._numcollected) + " items" |
| 263 |
if errors: |
| 264 |
line += " / %d errors" % errors |
| 265 |
if skipped: |
| 266 |
line += " / %d skipped" % skipped |
| 267 |
if self.isatty: |
| 268 |
if final: |
| 269 |
line += " \n" |
| 270 |
self.rewrite(line, bold=True) |
| 271 |
else: |
| 272 |
self.write_line(line) |
| 273 |
|
| 274 |
def pytest_collection_modifyitems(self): |
| 275 |
self.report_collect(True) |
| 276 |
|
| 277 |
@pytest.hookimpl(trylast=True) |
| 278 |
def pytest_sessionstart(self, session): |
| 279 |
self._sessionstarttime = time.time() |
| 280 |
if not self.showheader: |
| 281 |
return |
| 282 |
self.write_sep("=", "test session starts", bold=True) |
| 283 |
verinfo = platform.python_version() |
| 284 |
msg = "platform %s -- Python %s" % (sys.platform, verinfo) |
| 285 |
if hasattr(sys, 'pypy_version_info'): |
| 286 |
verinfo = ".".join(map(str, sys.pypy_version_info[:3])) |
| 287 |
msg += "[pypy-%s-%s]" % (verinfo, sys.pypy_version_info[3]) |
| 288 |
msg += ", pytest-%s, py-%s, pluggy-%s" % ( |
| 289 |
pytest.__version__, py.__version__, pluggy.__version__) |
| 290 |
if self.verbosity > 0 or self.config.option.debug or \ |
| 291 |
getattr(self.config.option, 'pastebin', None): |
| 292 |
msg += " -- " + str(sys.executable) |
| 293 |
self.write_line(msg) |
| 294 |
lines = self.config.hook.pytest_report_header( |
| 295 |
config=self.config, startdir=self.startdir) |
| 296 |
lines.reverse() |
| 297 |
for line in flatten(lines): |
| 298 |
self.write_line(line) |
| 299 |
|
| 300 |
def pytest_report_header(self, config): |
| 301 |
inifile = "" |
| 302 |
if config.inifile: |
| 303 |
inifile = config.rootdir.bestrelpath(config.inifile) |
| 304 |
lines = ["rootdir: %s, inifile: %s" %(config.rootdir, inifile)] |
| 305 |
|
| 306 |
plugininfo = config.pluginmanager.list_plugin_distinfo() |
| 307 |
if plugininfo: |
| 308 |
|
| 309 |
lines.append( |
| 310 |
"plugins: %s" % ", ".join(_plugin_nameversions(plugininfo))) |
| 311 |
return lines |
| 312 |
|
| 313 |
def pytest_collection_finish(self, session): |
| 314 |
if self.config.option.collectonly: |
| 315 |
self._printcollecteditems(session.items) |
| 316 |
if self.stats.get('failed'): |
| 317 |
self._tw.sep("!", "collection failures") |
| 318 |
for rep in self.stats.get('failed'): |
| 319 |
rep.toterminal(self._tw) |
| 320 |
return 1 |
| 321 |
return 0 |
| 322 |
if not self.showheader: |
| 323 |
return |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
def _printcollecteditems(self, items): |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if self.config.option.verbose < 0: |
| 332 |
if self.config.option.verbose < -1: |
| 333 |
counts = {} |
| 334 |
for item in items: |
| 335 |
name = item.nodeid.split('::', 1)[0] |
| 336 |
counts[name] = counts.get(name, 0) + 1 |
| 337 |
for name, count in sorted(counts.items()): |
| 338 |
self._tw.line("%s: %d" % (name, count)) |
| 339 |
else: |
| 340 |
for item in items: |
| 341 |
nodeid = item.nodeid |
| 342 |
nodeid = nodeid.replace("::()::", "::") |
| 343 |
self._tw.line(nodeid) |
| 344 |
return |
| 345 |
stack = [] |
| 346 |
indent = "" |
| 347 |
for item in items: |
| 348 |
needed_collectors = item.listchain()[1:] |
| 349 |
while stack: |
| 350 |
if stack == needed_collectors[:len(stack)]: |
| 351 |
break |
| 352 |
stack.pop() |
| 353 |
for col in needed_collectors[len(stack):]: |
| 354 |
stack.append(col) |
| 355 |
|
| 356 |
|
| 357 |
indent = (len(stack) - 1) * " " |
| 358 |
self._tw.line("%s%s" % (indent, col)) |
| 359 |
|
| 360 |
@pytest.hookimpl(hookwrapper=True) |
| 361 |
def pytest_sessionfinish(self, exitstatus): |
| 362 |
outcome = yield |
| 363 |
outcome.get_result() |
| 364 |
self._tw.line("") |
| 365 |
summary_exit_codes = ( |
| 366 |
EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, EXIT_USAGEERROR, |
| 367 |
EXIT_NOTESTSCOLLECTED) |
| 368 |
if exitstatus in summary_exit_codes: |
| 369 |
self.summary_errors() |
| 370 |
self.summary_failures() |
| 371 |
self.summary_warnings() |
| 372 |
self.summary_passes() |
| 373 |
self.config.hook.pytest_terminal_summary(terminalreporter=self) |
| 374 |
if exitstatus == EXIT_INTERRUPTED: |
| 375 |
self._report_keyboardinterrupt() |
| 376 |
del self._keyboardinterrupt_memo |
| 377 |
self.summary_deselected() |
| 378 |
self.summary_stats() |
| 379 |
|
| 380 |
def pytest_keyboard_interrupt(self, excinfo): |
| 381 |
self._keyboardinterrupt_memo = excinfo.getrepr(funcargs=True) |
| 382 |
|
| 383 |
def pytest_unconfigure(self): |
| 384 |
if hasattr(self, '_keyboardinterrupt_memo'): |
| 385 |
self._report_keyboardinterrupt() |
| 386 |
|
| 387 |
def _report_keyboardinterrupt(self): |
| 388 |
excrepr = self._keyboardinterrupt_memo |
| 389 |
msg = excrepr.reprcrash.message |
| 390 |
self.write_sep("!", msg) |
| 391 |
if "KeyboardInterrupt" in msg: |
| 392 |
if self.config.option.fulltrace: |
| 393 |
excrepr.toterminal(self._tw) |
| 394 |
else: |
| 395 |
self._tw.line("to show a full traceback on KeyboardInterrupt use --fulltrace", yellow=True) |
| 396 |
excrepr.reprcrash.toterminal(self._tw) |
| 397 |
|
| 398 |
def _locationline(self, nodeid, fspath, lineno, domain): |
| 399 |
def mkrel(nodeid): |
| 400 |
line = self.config.cwd_relative_nodeid(nodeid) |
| 401 |
if domain and line.endswith(domain): |
| 402 |
line = line[:-len(domain)] |
| 403 |
l = domain.split("[") |
| 404 |
l[0] = l[0].replace('.', '::') |
| 405 |
line += "[".join(l) |
| 406 |
return line |
| 407 |
|
| 408 |
|
| 409 |
if fspath: |
| 410 |
res = mkrel(nodeid).replace("::()", "") |
| 411 |
if nodeid.split("::")[0] != fspath.replace("\\", "/"): |
| 412 |
res += " <- " + self.startdir.bestrelpath(fspath) |
| 413 |
else: |
| 414 |
res = "[location]" |
| 415 |
return res + " " |
| 416 |
|
| 417 |
def _getfailureheadline(self, rep): |
| 418 |
if hasattr(rep, 'location'): |
| 419 |
fspath, lineno, domain = rep.location |
| 420 |
return domain |
| 421 |
else: |
| 422 |
return "test session" |
| 423 |
|
| 424 |
def _getcrashline(self, rep): |
| 425 |
try: |
| 426 |
return str(rep.longrepr.reprcrash) |
| 427 |
except AttributeError: |
| 428 |
try: |
| 429 |
return str(rep.longrepr)[:50] |
| 430 |
except AttributeError: |
| 431 |
return "" |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
def getreports(self, name): |
| 437 |
l = [] |
| 438 |
for x in self.stats.get(name, []): |
| 439 |
if not hasattr(x, '_pdbshown'): |
| 440 |
l.append(x) |
| 441 |
return l |
| 442 |
|
| 443 |
def summary_warnings(self): |
| 444 |
if self.hasopt("w"): |
| 445 |
warnings = self.stats.get("warnings") |
| 446 |
if not warnings: |
| 447 |
return |
| 448 |
self.write_sep("=", "pytest-warning summary") |
| 449 |
for w in warnings: |
| 450 |
self._tw.line("W%s %s %s" % (w.code, |
| 451 |
w.fslocation, w.message)) |
| 452 |
|
| 453 |
def summary_passes(self): |
| 454 |
if self.config.option.tbstyle != "no": |
| 455 |
if self.hasopt("P"): |
| 456 |
reports = self.getreports('passed') |
| 457 |
if not reports: |
| 458 |
return |
| 459 |
self.write_sep("=", "PASSES") |
| 460 |
for rep in reports: |
| 461 |
msg = self._getfailureheadline(rep) |
| 462 |
self.write_sep("_", msg) |
| 463 |
self._outrep_summary(rep) |
| 464 |
|
| 465 |
def summary_failures(self): |
| 466 |
if self.config.option.tbstyle != "no": |
| 467 |
reports = self.getreports('failed') |
| 468 |
if not reports: |
| 469 |
return |
| 470 |
self.write_sep("=", "FAILURES") |
| 471 |
for rep in reports: |
| 472 |
if self.config.option.tbstyle == "line": |
| 473 |
line = self._getcrashline(rep) |
| 474 |
self.write_line(line) |
| 475 |
else: |
| 476 |
msg = self._getfailureheadline(rep) |
| 477 |
markup = {'red': True, 'bold': True} |
| 478 |
self.write_sep("_", msg, **markup) |
| 479 |
self._outrep_summary(rep) |
| 480 |
|
| 481 |
def summary_errors(self): |
| 482 |
if self.config.option.tbstyle != "no": |
| 483 |
reports = self.getreports('error') |
| 484 |
if not reports: |
| 485 |
return |
| 486 |
self.write_sep("=", "ERRORS") |
| 487 |
for rep in self.stats['error']: |
| 488 |
msg = self._getfailureheadline(rep) |
| 489 |
if not hasattr(rep, 'when'): |
| 490 |
|
| 491 |
msg = "ERROR collecting " + msg |
| 492 |
elif rep.when == "setup": |
| 493 |
msg = "ERROR at setup of " + msg |
| 494 |
elif rep.when == "teardown": |
| 495 |
msg = "ERROR at teardown of " + msg |
| 496 |
self.write_sep("_", msg) |
| 497 |
self._outrep_summary(rep) |
| 498 |
|
| 499 |
def _outrep_summary(self, rep): |
| 500 |
rep.toterminal(self._tw) |
| 501 |
for secname, content in rep.sections: |
| 502 |
self._tw.sep("-", secname) |
| 503 |
if content[-1:] == "\n": |
| 504 |
content = content[:-1] |
| 505 |
self._tw.line(content) |
| 506 |
|
| 507 |
def summary_stats(self): |
| 508 |
session_duration = time.time() - self._sessionstarttime |
| 509 |
(line, color) = build_summary_stats_line(self.stats) |
| 510 |
msg = "%s in %.2f seconds" % (line, session_duration) |
| 511 |
markup = {color: True, 'bold': True} |
| 512 |
|
| 513 |
if self.verbosity >= 0: |
| 514 |
self.write_sep("=", msg, **markup) |
| 515 |
if self.verbosity == -1: |
| 516 |
self.write_line(msg, **markup) |
| 517 |
|
| 518 |
def summary_deselected(self): |
| 519 |
if 'deselected' in self.stats: |
| 520 |
l = [] |
| 521 |
k = self.config.option.keyword |
| 522 |
if k: |
| 523 |
l.append("-k%s" % k) |
| 524 |
m = self.config.option.markexpr |
| 525 |
if m: |
| 526 |
l.append("-m %r" % m) |
| 527 |
if l: |
| 528 |
self.write_sep("=", "%d tests deselected by %r" % ( |
| 529 |
len(self.stats['deselected']), " ".join(l)), bold=True) |
| 530 |
|
| 531 |
def repr_pythonversion(v=None): |
| 532 |
if v is None: |
| 533 |
v = sys.version_info |
| 534 |
try: |
| 535 |
return "%s.%s.%s-%s-%s" % v |
| 536 |
except (TypeError, ValueError): |
| 537 |
return str(v) |
| 538 |
|
| 539 |
def flatten(l): |
| 540 |
for x in l: |
| 541 |
if isinstance(x, (list, tuple)): |
| 542 |
for y in flatten(x): |
| 543 |
yield y |
| 544 |
else: |
| 545 |
yield x |
| 546 |
|
| 547 |
def build_summary_stats_line(stats): |
| 548 |
keys = ("failed passed skipped deselected " |
| 549 |
"xfailed xpassed warnings error").split() |
| 550 |
key_translation = {'warnings': 'pytest-warnings'} |
| 551 |
unknown_key_seen = False |
| 552 |
for key in stats.keys(): |
| 553 |
if key not in keys: |
| 554 |
if key: |
| 555 |
keys.append(key) |
| 556 |
unknown_key_seen = True |
| 557 |
parts = [] |
| 558 |
for key in keys: |
| 559 |
val = stats.get(key, None) |
| 560 |
if val: |
| 561 |
key_name = key_translation.get(key, key) |
| 562 |
parts.append("%d %s" % (len(val), key_name)) |
| 563 |
|
| 564 |
if parts: |
| 565 |
line = ", ".join(parts) |
| 566 |
else: |
| 567 |
line = "no tests ran" |
| 568 |
|
| 569 |
if 'failed' in stats or 'error' in stats: |
| 570 |
color = 'red' |
| 571 |
elif 'warnings' in stats or unknown_key_seen: |
| 572 |
color = 'yellow' |
| 573 |
elif 'passed' in stats: |
| 574 |
color = 'green' |
| 575 |
else: |
| 576 |
color = 'yellow' |
| 577 |
|
| 578 |
return (line, color) |
| 579 |
|
| 580 |
|
| 581 |
def _plugin_nameversions(plugininfo): |
| 582 |
l = [] |
| 583 |
for plugin, dist in plugininfo: |
| 584 |
|
| 585 |
name = '{dist.project_name}-{dist.version}'.format(dist=dist) |
| 586 |
|
| 587 |
if name.startswith("pytest-"): |
| 588 |
name = name[7:] |
| 589 |
|
| 590 |
|
| 591 |
if name not in l: |
| 592 |
l.append(name) |
| 593 |
return l |