Update conftest.py
This commit is contained in:
+63
-58
@@ -1,10 +1,59 @@
|
||||
# conftest.py
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import sys
|
||||
'''
|
||||
def safe_serialize(obj):
|
||||
try:
|
||||
json.dumps(obj)
|
||||
return obj
|
||||
except TypeError:
|
||||
try:
|
||||
return str(obj)
|
||||
except Exception:
|
||||
return "<unserializable>"
|
||||
|
||||
def serialize_object(obj):
|
||||
result = {}
|
||||
for attr in dir(obj):
|
||||
if attr.startswith("_"):
|
||||
continue
|
||||
try:
|
||||
value = getattr(obj, attr)
|
||||
result[attr] = safe_serialize(value)
|
||||
except Exception:
|
||||
result[attr] = "<error>"
|
||||
return result
|
||||
|
||||
# Conteneurs pour stocker les dumps
|
||||
all_data = {
|
||||
"session": {},
|
||||
"tests": [] # chaque test contiendra item + call
|
||||
}
|
||||
|
||||
def pytest_sessionstart(session):
|
||||
all_data["session"] = serialize_object(session)
|
||||
|
||||
def pytest_runtest_makereport(item, call):
|
||||
if call.when != "call":
|
||||
return
|
||||
item_dump = serialize_object(item)
|
||||
call_dump = serialize_object(call)
|
||||
all_data["tests"].append({
|
||||
"nodeid": item.nodeid,
|
||||
"item": item_dump,
|
||||
"call": call_dump
|
||||
})
|
||||
|
||||
def pytest_sessionfinish(session, exitstatus):
|
||||
os.makedirs("ci-reports/markdown", exist_ok=True)
|
||||
print("✅ Dumping runtime_params.json...")
|
||||
with open("ci-reports/markdown/runtime_params.json", "w") as f:
|
||||
json.dump(all_data, f, indent=2)
|
||||
'''
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
# ---------- Safe serialization ----------
|
||||
def safe_serialize(obj):
|
||||
try:
|
||||
json.dumps(obj)
|
||||
@@ -17,6 +66,8 @@ def safe_serialize(obj):
|
||||
|
||||
def serialize_callspec_between_getparam_and_setmulti(callspec):
|
||||
data = {}
|
||||
|
||||
# Inclure uniquement ce qui est utile
|
||||
if hasattr(callspec, "params"):
|
||||
params = callspec.params
|
||||
if isinstance(params, dict):
|
||||
@@ -24,6 +75,7 @@ def serialize_callspec_between_getparam_and_setmulti(callspec):
|
||||
else:
|
||||
data["params"] = safe_serialize(params)
|
||||
|
||||
# Tu peux ajouter d'autres attributs utiles ici si besoin
|
||||
for attr in ["id", "param_index"]:
|
||||
if hasattr(callspec, attr):
|
||||
try:
|
||||
@@ -35,70 +87,23 @@ def serialize_callspec_between_getparam_and_setmulti(callspec):
|
||||
|
||||
return data
|
||||
|
||||
# ---------- Runtime params ----------
|
||||
runtime_params = []
|
||||
|
||||
def pytest_runtest_makereport(item, call):
|
||||
if call.when != "call":
|
||||
return
|
||||
entry = {"nodeid": item.nodeid, "callspec": None}
|
||||
|
||||
entry = {
|
||||
"nodeid": item.nodeid,
|
||||
"callspec": None
|
||||
}
|
||||
|
||||
if hasattr(item, "callspec"):
|
||||
entry["callspec"] = serialize_callspec_between_getparam_and_setmulti(item.callspec)
|
||||
|
||||
runtime_params.append(entry)
|
||||
|
||||
# ---------- Capture prints ----------
|
||||
'''
|
||||
LOGFILE = pathlib.Path("outputs/test-prints.log")
|
||||
|
||||
def pytest_sessionstart(session):
|
||||
LOGFILE.parent.mkdir(parents=True, exist_ok=True)
|
||||
LOGFILE.write_text("") # reset à chaque run
|
||||
|
||||
def pytest_runtest_logreport(report):
|
||||
if report.when != "call": # éviter doublons
|
||||
return
|
||||
|
||||
if report.capstdout and report.capstdout.strip():
|
||||
with LOGFILE.open("a") as f:
|
||||
f.write(
|
||||
f"\n<span style='color:purple;font-weight:bold'>--- {report.nodeid} (stdout) ---</span>\n"
|
||||
)
|
||||
f.write(report.capstdout)
|
||||
|
||||
if report.capstderr and report.capstderr.strip():
|
||||
with LOGFILE.open("a") as f:
|
||||
f.write(
|
||||
f"\n<span style='color:purple;font-weight:bold'>--- {report.nodeid} (stderr) ---</span>\n"
|
||||
)
|
||||
f.write(report.capstderr)
|
||||
|
||||
# ---------- Capture global/external output ----------
|
||||
class TeeStdout:
|
||||
def __init__(self, original):
|
||||
self.original = original
|
||||
self.buffer = ""
|
||||
|
||||
def write(self, data):
|
||||
self.original.write(data)
|
||||
self.buffer += data
|
||||
if "\n" in self.buffer:
|
||||
chunk, self.buffer = self.buffer.split("\n", 1)
|
||||
if chunk.strip():
|
||||
with LOGFILE.open("a") as f:
|
||||
f.write(
|
||||
f"\n<span style='color:purple;font-weight:bold'>"
|
||||
f"--- [EXTERNAL PROCESS OUTPUT] ---</span>\n"
|
||||
)
|
||||
f.write(chunk + "\n")
|
||||
|
||||
def flush(self):
|
||||
self.original.flush()
|
||||
'''
|
||||
def pytest_configure(config):
|
||||
sys.stdout = TeeStdout(sys.stdout)
|
||||
sys.stderr = TeeStdout(sys.stderr)
|
||||
|
||||
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)
|
||||
json.dump(runtime_params, f, indent=2)
|
||||
Reference in New Issue
Block a user