From 9cf0bf1c8a9badfdc6b79b112e41c565c1e1dc59 Mon Sep 17 00:00:00 2001 From: tligui_y Date: Tue, 15 Jul 2025 13:33:30 +0200 Subject: [PATCH] Update json_to_md.py --- json_to_md.py | 73 +++++++++++---------------------------------------- 1 file changed, 16 insertions(+), 57 deletions(-) diff --git a/json_to_md.py b/json_to_md.py index a78fbad1c..43527daed 100644 --- a/json_to_md.py +++ b/json_to_md.py @@ -18,7 +18,7 @@ def stringify(obj): return f"`{str(obj)}`" def normalize_nodeid(nodeid): - """Convert pytest nodeid to Allure fullName format""" + """Convert pytest nodeid to a compact id if needed (for function name grouping)""" match = re.match(r"(tests[/\\].+?)\.py::(.+?)(?:\[.*)?$", nodeid) if match: file_part = match.group(1).replace("/", ".").replace("\\", ".") @@ -26,6 +26,15 @@ def normalize_nodeid(nodeid): return f"{file_part}#{func_part}" return None +def extract_param_str_from_nodeid(nodeid): + """Extract parameter string from nodeid, e.g. test[x-y-z] → x-y-z. + Handles nested or inner brackets.""" + first = nodeid.find('[') + last = nodeid.rfind(']') + if first != -1 and last != -1 and last > first: + return nodeid[first+1:last] + return None + def write_details(f, summary, body, level=0): margin = 18 * level # 18px de marge/indent par niveau border = f"border-left: 2px solid #eee;" if level > 0 else "" @@ -35,69 +44,20 @@ def write_details(f, summary, body, level=0): f.write("\n") f.write("\n\n") -def load_allure_metadata(allure_test_cases_dir): - allure_data = {} - if not os.path.exists(allure_test_cases_dir): - print(f"❌ Allure directory not found: {allure_test_cases_dir}") - return allure_data - - print(f"Loading Allure files from: {allure_test_cases_dir}") - for file in os.listdir(allure_test_cases_dir): - # Ignore temp/hidden/invalid files, just in case - if not file.endswith(".json") or file.startswith("."): - continue - path = os.path.join(allure_test_cases_dir, file) - # Si le fichier est vide ou absent (par exemple pas collecté), on skippe - if not os.path.isfile(path) or os.path.getsize(path) == 0: - print(f"Allure file: {file} skipped (not found or empty).") - continue - try: - with open(path, 'r', encoding='utf-8') as f: - data = json.load(f) - full_name = data.get("fullName") - if not full_name: - print(f"Allure file: {file} skipped (no fullName).") - continue - params = data.get("parameters", []) - severity = data.get("extra", {}).get("severity", None) - allure_data[full_name] = { - "parameters": params, - "severity": severity - } - print(f"Allure file: {file} loaded ✔️") - - - except Exception as e: - short_summary = traceback.format_exception_only(type(e), e)[-1].strip() - print(f"Allure file: {file} failed ❌ (reason: {short_summary})") - - return allure_data - - -def json_to_md_nested(json_path, md_path, allure_dir=None): +def json_to_md_nested(json_path, md_path): with open(json_path) as f: data = json.load(f) - allure_data = load_allure_metadata(allure_dir) if allure_dir else {} - def make_test_block(test, status, emoji, level): nodeid = test.get("nodeid", "") + param_str = extract_param_str_from_nodeid(nodeid) body_test = f"- **Status:** {emoji} `{status}`\n" duration = test.get("call", {}).get("duration") body_test += f"- **Duration:** `{duration:.6f}` s\n" if duration else "- **Duration:** `None`\n" - full_name = normalize_nodeid(nodeid) - allure_info = allure_data.get(full_name) - if allure_info: - if allure_info["parameters"]: - body_test += "- **Parameters (Allure):**\n" - for param in allure_info["parameters"]: - name = param.get("name") - val = param.get("value") - body_test += f" - `{name}` = `{val}`\n" - body_test += "\n" - if allure_info["severity"]: - body_test += f"- **Severity:** `{allure_info['severity']}`\n" + # Ajout de la ligne parameters si présents + if param_str: + body_test += f"- **Parameters:** `{param_str}`\n" for phase in ['setup', 'call', 'teardown']: if phase in test: @@ -326,13 +286,12 @@ def main(): parser = argparse.ArgumentParser(description="Convert JSON test results to Markdown.") parser.add_argument("--input", required=True, help="Path to pytest JSON file") parser.add_argument("--output", required=True, help="Path to output Markdown file") - parser.add_argument("--allure-dir", required=False, help="Directory of Allure test-cases (optional)") parser.add_argument("--log", required=False, help="Path to raw pytest output log (optional)") parser.add_argument("--json", required=False, help="Path to pytest-report.json") args = parser.parse_args() - json_to_md_nested(args.input, args.output, args.allure_dir) + json_to_md_nested(args.input, args.output) run_pytest_and_generate_banner_with_logs(md_path=args.output, log_path=args.log) print(f"✅ Report generated at {args.output}")