|
16 | 16 | from functools import lru_cache |
17 | 17 | from html import escape |
18 | 18 | from os.path import isfile |
19 | | -from typing import Any |
20 | | -from typing import Dict |
21 | 19 |
|
22 | 20 | import pytest |
23 | 21 | from _pytest.logging import _remove_ansi_escape_sequences |
|
28 | 26 | from . import __pypi_url__ |
29 | 27 | from . import __version__ |
30 | 28 | from . import extras |
| 29 | +from . import nextgen |
31 | 30 |
|
32 | 31 |
|
33 | 32 | @lru_cache() |
@@ -104,7 +103,7 @@ def pytest_configure(config): |
104 | 103 | if not hasattr(config, "workerinput"): |
105 | 104 | # prevent opening htmlpath on worker nodes (xdist) |
106 | 105 | config._html = HTMLReport(htmlpath, config) |
107 | | - config._next_gen = NextGenReport(config, Path("nextgendata.json")) |
| 106 | + config._next_gen = nextgen.NextGenReport(config, Path("nextgendata.js")) |
108 | 107 | config.pluginmanager.register(config._html) |
109 | 108 | config.pluginmanager.register(config._next_gen) |
110 | 109 |
|
@@ -767,64 +766,3 @@ def pytest_sessionfinish(self, session): |
767 | 766 |
|
768 | 767 | def pytest_terminal_summary(self, terminalreporter): |
769 | 768 | terminalreporter.write_sep("-", f"generated html file: file://{self.logfile}") |
770 | | - |
771 | | - |
772 | | -class NextGenReport: |
773 | | - def __init__(self, config, data_file): |
774 | | - self._config = config |
775 | | - self._data_file = data_file |
776 | | - |
777 | | - self._title = "Next Gen Report" |
778 | | - self._data = { |
779 | | - "title": self._title, |
780 | | - "collectedItems": 0, |
781 | | - "environment": {}, |
782 | | - "tests": [], |
783 | | - } |
784 | | - |
785 | | - self._data_file.parent.mkdir(parents=True, exist_ok=True) |
786 | | - |
787 | | - def _write(self): |
788 | | - try: |
789 | | - data = json.dumps(self._data) |
790 | | - except TypeError: |
791 | | - data = cleanup_unserializable(self._data) |
792 | | - data = json.dumps(data) |
793 | | - |
794 | | - with self._data_file.open("w", buffering=1, encoding="UTF-8") as f: |
795 | | - f.write("const jsonData = ") |
796 | | - f.write(data) |
797 | | - f.write("\n") |
798 | | - |
799 | | - @pytest.hookimpl(trylast=True) |
800 | | - def pytest_sessionstart(self, session): |
801 | | - config = session.config |
802 | | - if hasattr(config, "_metadata") and config._metadata: |
803 | | - metadata = config._metadata |
804 | | - self._data["environment"] = metadata |
805 | | - self._write() |
806 | | - |
807 | | - @pytest.hookimpl(trylast=True) |
808 | | - def pytest_collection_finish(self, session): |
809 | | - self._data["collectedItems"] = len(session.items) |
810 | | - self._write() |
811 | | - |
812 | | - @pytest.hookimpl(trylast=True) |
813 | | - def pytest_runtest_logreport(self, report): |
814 | | - data = self._config.hook.pytest_report_to_serializable( |
815 | | - config=self._config, report=report |
816 | | - ) |
817 | | - self._data["tests"].append(data) |
818 | | - self._write() |
819 | | - |
820 | | - |
821 | | -def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]: |
822 | | - """Return new dict with entries that are not json serializable by their str().""" |
823 | | - result = {} |
824 | | - for k, v in d.items(): |
825 | | - try: |
826 | | - json.dumps({k: v}) |
827 | | - except TypeError: |
828 | | - v = str(v) |
829 | | - result[k] = v |
830 | | - return result |
0 commit comments