|
| 1 | +import argparse |
| 2 | +import csv |
| 3 | +import sys |
| 4 | + |
| 5 | +# |
| 6 | +# A standalone script to generate a Markdown representation of a test report. |
| 7 | +# This is primarily intended to be used with GitHub actions to generate a nice |
| 8 | +# representation of the test results when looking at the action run. |
| 9 | +# |
| 10 | +# Usage: python executorch/backends/test/suite/generate_markdown_summary.py <path to test report CSV file> |
| 11 | +# Markdown is written to stdout. |
| 12 | +# |
| 13 | + |
| 14 | +def generate_markdown(csv_path: str, exit_code: int = 0): |
| 15 | + # Print warning if exit code is non-zero |
| 16 | + if exit_code != 0: |
| 17 | + print(f"> [!WARNING]") |
| 18 | + print(f"> Exit code {exit_code} was non-zero. Test process may have crashed. Check the job logs for more information.\n") |
| 19 | + |
| 20 | + with open(csv_path, newline='', encoding='utf-8') as f: |
| 21 | + reader = csv.reader(f) |
| 22 | + rows = list(reader) |
| 23 | + |
| 24 | + header = rows[0] |
| 25 | + data_rows = rows[1:] |
| 26 | + |
| 27 | + # Find the Result and Result Detail column indices |
| 28 | + result_column_index = None |
| 29 | + result_detail_column_index = None |
| 30 | + for i, col in enumerate(header): |
| 31 | + if col.lower() == 'result': |
| 32 | + result_column_index = i |
| 33 | + elif col.lower() == 'result detail': |
| 34 | + result_detail_column_index = i |
| 35 | + |
| 36 | + # Count results and prepare data |
| 37 | + pass_count = 0 |
| 38 | + fail_count = 0 |
| 39 | + skip_count = 0 |
| 40 | + failed_tests = [] |
| 41 | + processed_rows = [] |
| 42 | + result_detail_counts = {} |
| 43 | + |
| 44 | + for row in data_rows: |
| 45 | + # Make a copy of the row to avoid modifying the original |
| 46 | + processed_row = row.copy() |
| 47 | + |
| 48 | + # Count results and collect failed tests |
| 49 | + if result_column_index is not None and result_column_index < len(row): |
| 50 | + result_value = row[result_column_index].strip().lower() |
| 51 | + if result_value == 'pass': |
| 52 | + pass_count += 1 |
| 53 | + processed_row[result_column_index] = '<span style="color:green">Pass</span>' |
| 54 | + elif result_value == 'fail': |
| 55 | + fail_count += 1 |
| 56 | + processed_row[result_column_index] = '<span style="color:red">Fail</span>' |
| 57 | + failed_tests.append(processed_row.copy()) |
| 58 | + elif result_value == 'skip': |
| 59 | + skip_count += 1 |
| 60 | + processed_row[result_column_index] = '<span style="color:gray">Skip</span>' |
| 61 | + |
| 62 | + # Count result details (excluding empty ones) |
| 63 | + if result_detail_column_index is not None and result_detail_column_index < len(row): |
| 64 | + result_detail_value = row[result_detail_column_index].strip() |
| 65 | + if result_detail_value: # Only count non-empty result details |
| 66 | + if result_detail_value in result_detail_counts: |
| 67 | + result_detail_counts[result_detail_value] += 1 |
| 68 | + else: |
| 69 | + result_detail_counts[result_detail_value] = 1 |
| 70 | + |
| 71 | + processed_rows.append(processed_row) |
| 72 | + |
| 73 | + # Generate Summary section |
| 74 | + total_rows = len(data_rows) |
| 75 | + print("# Summary\n") |
| 76 | + print(f"- **Pass**: {pass_count}/{total_rows}") |
| 77 | + print(f"- **Fail**: {fail_count}/{total_rows}") |
| 78 | + print(f"- **Skip**: {skip_count}/{total_rows}") |
| 79 | + |
| 80 | + print("## Failure Breakdown:") |
| 81 | + total_rows_with_result_detail = sum(result_detail_counts.values()) |
| 82 | + for detail, count in sorted(result_detail_counts.items()): |
| 83 | + print(f"- **{detail}**: {count}/{total_rows_with_result_detail}") |
| 84 | + |
| 85 | + # Generate Failed Tests section |
| 86 | + print("# Failed Tests\n") |
| 87 | + if failed_tests: |
| 88 | + print("| " + " | ".join(header) + " |") |
| 89 | + print("|" + "|".join(['---'] * len(header)) + "|") |
| 90 | + for row in failed_tests: |
| 91 | + print("| " + " | ".join(row) + " |") |
| 92 | + else: |
| 93 | + print("No failed tests.\n") |
| 94 | + print() |
| 95 | + |
| 96 | + # Generate Test Cases section |
| 97 | + print("# Test Cases\n") |
| 98 | + print("| " + " | ".join(header) + " |") |
| 99 | + print("|" + "|".join(['---'] * len(header)) + "|") |
| 100 | + for row in processed_rows: |
| 101 | + print("| " + " | ".join(row) + " |") |
| 102 | + |
| 103 | +def main(): |
| 104 | + parser = argparse.ArgumentParser(description="Generate a Markdown representation of a test report.") |
| 105 | + parser.add_argument("csv_path", help="Path to the test report CSV file.") |
| 106 | + parser.add_argument("--exit_code", type=int, default=0, help="Exit code from the test process.") |
| 107 | + args = parser.parse_args() |
| 108 | + try: |
| 109 | + generate_markdown(args.csv_path, args.exit_code) |
| 110 | + except Exception as e: |
| 111 | + print(f"Error: {e}", file=sys.stderr) |
| 112 | + sys.exit(1) |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments