63 lines
1.9 KiB
Python
63 lines
1.9 KiB
Python
# conftest.py
|
|
import json, os, pathlib
|
|
|
|
def safe_serialize(obj):
|
|
try:
|
|
json.dumps(obj)
|
|
return obj
|
|
except TypeError:
|
|
try:
|
|
return str(obj)
|
|
except Exception:
|
|
return "<unserializable>"
|
|
|
|
def serialize_callspec_between_getparam_and_setmulti(callspec):
|
|
data = {}
|
|
if hasattr(callspec, "params"):
|
|
params = callspec.params
|
|
if isinstance(params, dict):
|
|
data["params"] = {str(k): safe_serialize(v) for k, v in params.items()}
|
|
else:
|
|
data["params"] = safe_serialize(params)
|
|
for attr in ["id", "param_index"]:
|
|
if hasattr(callspec, attr):
|
|
try:
|
|
value = getattr(callspec, attr)
|
|
if not callable(value):
|
|
data[attr] = safe_serialize(value)
|
|
except Exception:
|
|
data[attr] = "<error>"
|
|
return data
|
|
|
|
# === PARAMETERS ===
|
|
|
|
runtime_params = []
|
|
|
|
def pytest_runtest_makereport(item, call):
|
|
if call.when != "call":
|
|
return
|
|
entry = {"nodeid": item.nodeid, "callspec": None}
|
|
if hasattr(item, "callspec"):
|
|
entry["callspec"] = serialize_callspec_between_getparam_and_setmulti(item.callspec)
|
|
runtime_params.append(entry)
|
|
|
|
# === LOG PRINTS ===
|
|
LOGFILE = pathlib.Path("outputs/test-prints.log")
|
|
|
|
def pytest_sessionstart(session):
|
|
LOGFILE.parent.mkdir(parents=True, exist_ok=True)
|
|
LOGFILE.touch(exist_ok=True)
|
|
|
|
def pytest_runtest_logreport(report):
|
|
if report.when != "call": # ignore setup et teardown
|
|
return
|
|
if report.capstdout and report.capstdout.strip():
|
|
with LOGFILE.open("a") as f:
|
|
f.write(f"\033[95m--- {report.nodeid} ---\033[0m\n")
|
|
f.write(report.capstdout + "\n")
|
|
|
|
def pytest_sessionfinish(session, exitstatus):
|
|
os.makedirs("markdown", exist_ok=True)
|
|
with open("markdown/runtime_params.json", "w") as f:
|
|
json.dump(runtime_params, f, indent=2)
|