100 lines
2.5 KiB
Python
100 lines
2.5 KiB
Python
# conftest.py
|
|
import json
|
|
import os
|
|
|
|
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)
|
|
'''
|
|
|
|
|
|
def safe_serialize(obj):
|
|
"""Rend n'importe quoi JSON-serializable."""
|
|
if isinstance(obj, (str, int, float, bool)) or obj is None:
|
|
return obj
|
|
if isinstance(obj, (list, tuple, set)):
|
|
return [safe_serialize(x) for x in obj]
|
|
if isinstance(obj, dict):
|
|
return {str(k): safe_serialize(v) for k, v in obj.items()}
|
|
try:
|
|
return str(obj)
|
|
except Exception:
|
|
return "<unserializable>"
|
|
|
|
def serialize_full_object(obj):
|
|
"""Serialize *tout* un objet, y compris les champs privés."""
|
|
result = {}
|
|
for attr in dir(obj):
|
|
try:
|
|
value = getattr(obj, attr)
|
|
result[attr] = safe_serialize(value)
|
|
except Exception:
|
|
result[attr] = "<error>"
|
|
return result
|
|
|
|
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_full_object(item.callspec)
|
|
|
|
runtime_params.append(entry)
|
|
|
|
def pytest_sessionfinish(session, exitstatus):
|
|
os.makedirs("ci-reports/markdown", exist_ok=True)
|
|
with open("ci-reports/markdown/runtime_params.json", "w") as f:
|
|
json.dump(runtime_params, f, indent=2)
|
|
|
|
''' |