| 1 |
""" log machine-parseable test session result information in a plain |
| 2 |
text file. |
| 3 |
""" |
| 4 |
|
| 5 |
import py |
| 6 |
import os |
| 7 |
import sys |
| 8 |
|
| 9 |
if sys.version_info >= (3, 0, 0): |
| 10 |
unicode = str |
| 11 |
|
| 12 |
def pytest_addoption(parser): |
| 13 |
group = parser.getgroup("terminal reporting", "resultlog plugin options") |
| 14 |
group.addoption('--resultlog', '--result-log', action="store", |
| 15 |
metavar="path", default=None, |
| 16 |
help="path for machine-readable result log.") |
| 17 |
|
| 18 |
def pytest_configure(config): |
| 19 |
resultlog = config.option.resultlog |
| 20 |
|
| 21 |
if resultlog and not hasattr(config, 'slaveinput'): |
| 22 |
dirname = os.path.dirname(os.path.abspath(resultlog)) |
| 23 |
if not os.path.isdir(dirname): |
| 24 |
os.makedirs(dirname) |
| 25 |
logfile = open(resultlog, 'w', 1) |
| 26 |
config._resultlog = ResultLog(config, logfile) |
| 27 |
config.pluginmanager.register(config._resultlog) |
| 28 |
|
| 29 |
def pytest_unconfigure(config): |
| 30 |
resultlog = getattr(config, '_resultlog', None) |
| 31 |
if resultlog: |
| 32 |
resultlog.logfile.close() |
| 33 |
del config._resultlog |
| 34 |
config.pluginmanager.unregister(resultlog) |
| 35 |
|
| 36 |
def generic_path(item): |
| 37 |
chain = item.listchain() |
| 38 |
gpath = [chain[0].name] |
| 39 |
fspath = chain[0].fspath |
| 40 |
fspart = False |
| 41 |
for node in chain[1:]: |
| 42 |
newfspath = node.fspath |
| 43 |
if newfspath == fspath: |
| 44 |
if fspart: |
| 45 |
gpath.append(':') |
| 46 |
fspart = False |
| 47 |
else: |
| 48 |
gpath.append('.') |
| 49 |
else: |
| 50 |
gpath.append('/') |
| 51 |
fspart = True |
| 52 |
name = node.name |
| 53 |
if name[0] in '([': |
| 54 |
gpath.pop() |
| 55 |
gpath.append(name) |
| 56 |
fspath = newfspath |
| 57 |
return ''.join(gpath) |
| 58 |
|
| 59 |
class ResultLog(object): |
| 60 |
def __init__(self, config, logfile): |
| 61 |
self.config = config |
| 62 |
self.logfile = logfile |
| 63 |
|
| 64 |
def write_log_entry(self, testpath, lettercode, longrepr, sections=None): |
| 65 |
_safeprint("%s %s" % (lettercode, testpath), file=self.logfile) |
| 66 |
for line in longrepr.splitlines(): |
| 67 |
_safeprint(" %s" % line, file=self.logfile) |
| 68 |
if sections is not None and ( |
| 69 |
lettercode in ('E', 'F')): |
| 70 |
for title, content in sections: |
| 71 |
_safeprint(" ---------- %s ----------" % (title,), |
| 72 |
file=self.logfile) |
| 73 |
for line in content.splitlines(): |
| 74 |
_safeprint(" %s" % line, file=self.logfile) |
| 75 |
|
| 76 |
def log_outcome(self, report, lettercode, longrepr): |
| 77 |
testpath = getattr(report, 'nodeid', None) |
| 78 |
if testpath is None: |
| 79 |
testpath = report.fspath |
| 80 |
self.write_log_entry(testpath, lettercode, longrepr, |
| 81 |
getattr(report, 'sections', None)) |
| 82 |
|
| 83 |
def pytest_runtest_logreport(self, report): |
| 84 |
if report.when != "call" and report.passed: |
| 85 |
return |
| 86 |
res = self.config.hook.pytest_report_teststatus(report=report) |
| 87 |
code = res[1] |
| 88 |
if code == 'x': |
| 89 |
longrepr = str(report.longrepr) |
| 90 |
elif code == 'X': |
| 91 |
longrepr = '' |
| 92 |
elif report.passed: |
| 93 |
longrepr = "" |
| 94 |
elif report.failed: |
| 95 |
longrepr = str(report.longrepr) |
| 96 |
elif report.skipped: |
| 97 |
longrepr = str(report.longrepr[2]) |
| 98 |
self.log_outcome(report, code, longrepr) |
| 99 |
|
| 100 |
def pytest_collectreport(self, report): |
| 101 |
if not report.passed: |
| 102 |
if report.failed: |
| 103 |
code = "F" |
| 104 |
longrepr = str(report.longrepr) |
| 105 |
else: |
| 106 |
assert report.skipped |
| 107 |
code = "s" |
| 108 |
longrepr = "%s:%d: %s" % report.longrepr |
| 109 |
self.log_outcome(report, code, longrepr) |
| 110 |
|
| 111 |
def pytest_internalerror(self, excrepr): |
| 112 |
reprcrash = getattr(excrepr, 'reprcrash', None) |
| 113 |
path = getattr(reprcrash, "path", None) |
| 114 |
if path is None: |
| 115 |
path = "cwd:%s" % py.path.local() |
| 116 |
self.write_log_entry(path, '!', str(excrepr)) |
| 117 |
|
| 118 |
def _safeprint(s, file): |
| 119 |
if isinstance(s, unicode): |
| 120 |
s = s.encode('utf-8') |
| 121 |
py.builtin.print_(s, file=file) |