Skip to content

Commit f143891

Browse files
committed
fix tests
1 parent 735433e commit f143891

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/pytest_html/plugin.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from functools import lru_cache
1717
from html import escape
1818
from os.path import isfile
19+
from typing import Any
20+
from typing import Dict
1921

2022
import pytest
2123
from _pytest.logging import _remove_ansi_escape_sequences
@@ -782,8 +784,15 @@ def __init__(self, config, data_file):
782784
self._data_file.parent.mkdir(parents=True, exist_ok=True)
783785

784786
def _write(self):
787+
try:
788+
data = json.dumps(self._data)
789+
except TypeError:
790+
data = cleanup_unserializable(self._data)
791+
data = json.dumps(data)
792+
785793
with self._data_file.open("w", buffering=1, encoding="UTF-8") as f:
786-
json.dump(self._data, f)
794+
f.write("const jsonData = ")
795+
f.write(data)
787796
f.write("\n")
788797

789798
@pytest.hookimpl(trylast=True)
@@ -801,3 +810,15 @@ def pytest_runtest_logreport(self, report):
801810
)
802811
self._data["tests"].append(data)
803812
self._write()
813+
814+
815+
def cleanup_unserializable(d: Dict[str, Any]) -> Dict[str, Any]:
816+
"""Return new dict with entries that are not json serializable by their str()."""
817+
result = {}
818+
for k, v in d.items():
819+
try:
820+
json.dumps({k: v})
821+
except TypeError:
822+
v = str(v)
823+
result[k] = v
824+
return result

0 commit comments

Comments
 (0)