move nextgen to sep module
This commit is contained in:
parent
6dc58cf632
commit
0f7a9e2ba5
|
@ -0,0 +1,66 @@
|
|||
import json
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class NextGenReport:
|
||||
def __init__(self, config, data_file):
|
||||
self._config = config
|
||||
self._data_file = data_file
|
||||
|
||||
self._title = "Next Gen Report"
|
||||
self._data = {
|
||||
"title": self._title,
|
||||
"collectedItems": 0,
|
||||
"environment": {},
|
||||
"tests": [],
|
||||
}
|
||||
|
||||
self._data_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _write(self):
|
||||
try:
|
||||
data = json.dumps(self._data)
|
||||
except TypeError:
|
||||
data = cleanup_unserializable(self._data)
|
||||
data = json.dumps(data)
|
||||
|
||||
with self._data_file.open("w", buffering=1, encoding="UTF-8") as f:
|
||||
f.write("const jsonData = ")
|
||||
f.write(data)
|
||||
f.write("\n")
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_sessionstart(self, session):
|
||||
config = session.config
|
||||
if hasattr(config, "_metadata") and config._metadata:
|
||||
metadata = config._metadata
|
||||
self._data["environment"] = metadata
|
||||
self._write()
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_collection_finish(self, session):
|
||||
self._data["collectedItems"] = len(session.items)
|
||||
self._write()
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_runtest_logreport(self, report):
|
||||
data = self._config.hook.pytest_report_to_serializable(
|
||||
config=self._config, report=report
|
||||
)
|
||||
self._data["tests"].append(data)
|
||||
self._write()
|
||||
|
||||
|
||||
def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Return new dict with entries that are not json serializable by their str()."""
|
||||
result = {}
|
||||
for k, v in d.items():
|
||||
try:
|
||||
json.dumps({k: v})
|
||||
except TypeError:
|
||||
v = str(v)
|
||||
result[k] = v
|
||||
return result
|
|
@ -16,8 +16,6 @@ from collections import OrderedDict
|
|||
from functools import lru_cache
|
||||
from html import escape
|
||||
from os.path import isfile
|
||||
from typing import Any
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
from _pytest.logging import _remove_ansi_escape_sequences
|
||||
|
@ -28,6 +26,7 @@ from py.xml import raw
|
|||
from . import __pypi_url__
|
||||
from . import __version__
|
||||
from . import extras
|
||||
from . import nextgen
|
||||
|
||||
|
||||
@lru_cache()
|
||||
|
@ -104,7 +103,7 @@ def pytest_configure(config):
|
|||
if not hasattr(config, "workerinput"):
|
||||
# prevent opening htmlpath on worker nodes (xdist)
|
||||
config._html = HTMLReport(htmlpath, config)
|
||||
config._next_gen = NextGenReport(config, Path("nextgendata.json"))
|
||||
config._next_gen = nextgen.NextGenReport(config, Path("nextgendata.js"))
|
||||
config.pluginmanager.register(config._html)
|
||||
config.pluginmanager.register(config._next_gen)
|
||||
|
||||
|
@ -767,64 +766,3 @@ class HTMLReport:
|
|||
|
||||
def pytest_terminal_summary(self, terminalreporter):
|
||||
terminalreporter.write_sep("-", f"generated html file: file://{self.logfile}")
|
||||
|
||||
|
||||
class NextGenReport:
|
||||
def __init__(self, config, data_file):
|
||||
self._config = config
|
||||
self._data_file = data_file
|
||||
|
||||
self._title = "Next Gen Report"
|
||||
self._data = {
|
||||
"title": self._title,
|
||||
"collectedItems": 0,
|
||||
"environment": {},
|
||||
"tests": [],
|
||||
}
|
||||
|
||||
self._data_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
def _write(self):
|
||||
try:
|
||||
data = json.dumps(self._data)
|
||||
except TypeError:
|
||||
data = cleanup_unserializable(self._data)
|
||||
data = json.dumps(data)
|
||||
|
||||
with self._data_file.open("w", buffering=1, encoding="UTF-8") as f:
|
||||
f.write("const jsonData = ")
|
||||
f.write(data)
|
||||
f.write("\n")
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_sessionstart(self, session):
|
||||
config = session.config
|
||||
if hasattr(config, "_metadata") and config._metadata:
|
||||
metadata = config._metadata
|
||||
self._data["environment"] = metadata
|
||||
self._write()
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_collection_finish(self, session):
|
||||
self._data["collectedItems"] = len(session.items)
|
||||
self._write()
|
||||
|
||||
@pytest.hookimpl(trylast=True)
|
||||
def pytest_runtest_logreport(self, report):
|
||||
data = self._config.hook.pytest_report_to_serializable(
|
||||
config=self._config, report=report
|
||||
)
|
||||
self._data["tests"].append(data)
|
||||
self._write()
|
||||
|
||||
|
||||
def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Return new dict with entries that are not json serializable by their str()."""
|
||||
result = {}
|
||||
for k, v in d.items():
|
||||
try:
|
||||
json.dumps({k: v})
|
||||
except TypeError:
|
||||
v = str(v)
|
||||
result[k] = v
|
||||
return result
|
||||
|
|
Loading…
Reference in New Issue