Introduce --report-log option

Fix #4488
This commit is contained in:
Bruno Oliveira
2019-10-16 19:24:41 -03:00
parent 0225cb37c0
commit b99661b9d7
10 changed files with 231 additions and 21 deletions

View File

@@ -154,6 +154,7 @@ default_plugins = essential_plugins + (
"assertion",
"junitxml",
"resultlog",
"report_log",
"doctest",
"cacheprovider",
"freeze_support",

72
src/_pytest/report_log.py Normal file
View File

@@ -0,0 +1,72 @@
import json
from pathlib import Path
import pytest
def pytest_addoption(parser):
group = parser.getgroup("terminal reporting", "report-log plugin options")
group.addoption(
"--report-log",
action="store",
metavar="path",
default=None,
help="Path to line-based json objects of test session events.",
)
def pytest_configure(config):
report_log = config.option.report_log
if report_log and not hasattr(config, "slaveinput"):
config._report_log_plugin = ReportLogPlugin(config, Path(report_log))
config.pluginmanager.register(config._report_log_plugin)
def pytest_unconfigure(config):
report_log_plugin = getattr(config, "_report_log_plugin", None)
if report_log_plugin:
report_log_plugin.close()
del config._report_log_plugin
class ReportLogPlugin:
def __init__(self, config, log_path: Path):
self._config = config
self._log_path = log_path
log_path.parent.mkdir(parents=True, exist_ok=True)
self._file = log_path.open("w", buffering=1, encoding="UTF-8")
def close(self):
if self._file is not None:
self._file.close()
self._file = None
def _write_json_data(self, data):
self._file.write(json.dumps(data) + "\n")
self._file.flush()
def pytest_sessionstart(self):
data = {"pytest_version": pytest.__version__, "$report_type": "SessionStart"}
self._write_json_data(data)
def pytest_sessionfinish(self, exitstatus):
data = {"exitstatus": exitstatus, "$report_type": "SessionFinish"}
self._write_json_data(data)
def pytest_runtest_logreport(self, report):
data = self._config.hook.pytest_report_to_serializable(
config=self._config, report=report
)
self._write_json_data(data)
def pytest_collectreport(self, report):
data = self._config.hook.pytest_report_to_serializable(
config=self._config, report=report
)
self._write_json_data(data)
def pytest_terminal_summary(self, terminalreporter):
terminalreporter.write_sep(
"-", "generated report log file: {}".format(self._log_path)
)

View File

@@ -329,18 +329,18 @@ class CollectErrorRepr(TerminalRepr):
def pytest_report_to_serializable(report):
if isinstance(report, (TestReport, CollectReport)):
data = report._to_json()
data["_report_type"] = report.__class__.__name__
data["$report_type"] = report.__class__.__name__
return data
def pytest_report_from_serializable(data):
if "_report_type" in data:
if data["_report_type"] == "TestReport":
if "$report_type" in data:
if data["$report_type"] == "TestReport":
return TestReport._from_json(data)
elif data["_report_type"] == "CollectReport":
elif data["$report_type"] == "CollectReport":
return CollectReport._from_json(data)
assert False, "Unknown report_type unserialize data: {}".format(
data["_report_type"]
data["$report_type"]
)