|
| 1 | +import argparse |
| 2 | +import csv |
| 3 | +import functools |
| 4 | +import json |
| 5 | +import sys |
| 6 | + |
| 7 | +from dataclasses import dataclass, field |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class ResultCounts: |
| 12 | + """ |
| 13 | + Represents aggregated result counts for each status. |
| 14 | + """ |
| 15 | + |
| 16 | + total: int = 0 |
| 17 | + passes: int = 0 |
| 18 | + fails: int = 0 |
| 19 | + skips: int = 0 |
| 20 | + by_detail: dict[str, int] = field(default_factory=lambda: {}) |
| 21 | + |
| 22 | + def add_row(self, result_value: str, result_detail: str) -> None: |
| 23 | + """ |
| 24 | + Update the result counts for the specified row. |
| 25 | + """ |
| 26 | + |
| 27 | + self.total += 1 |
| 28 | + |
| 29 | + if result_value == "Pass": |
| 30 | + self.passes += 1 |
| 31 | + elif result_value == "Fail": |
| 32 | + self.fails += 1 |
| 33 | + elif result_value == "Skip": |
| 34 | + self.skips += 1 |
| 35 | + else: |
| 36 | + raise RuntimeError(f"Unknown result value {result_value}") |
| 37 | + |
| 38 | + if result_detail: |
| 39 | + if result_detail not in self.by_detail: |
| 40 | + self.by_detail[result_detail] = 0 |
| 41 | + |
| 42 | + self.by_detail[result_detail] += 1 |
| 43 | + |
| 44 | + |
| 45 | +@dataclass |
| 46 | +class AggregatedSummary: |
| 47 | + """ |
| 48 | + Represents aggegrated summary data for the test run. |
| 49 | + """ |
| 50 | + |
| 51 | + counts: ResultCounts |
| 52 | + counts_by_params: dict[str, ResultCounts] |
| 53 | + failed_tests: list[list[str]] |
| 54 | + |
| 55 | + |
| 56 | +# |
| 57 | +# A standalone script to generate a Markdown representation of a test report. |
| 58 | +# This is primarily intended to be used with GitHub actions to generate a nice |
| 59 | +# representation of the test results when looking at the action run. |
| 60 | +# |
| 61 | +# Usage: python executorch/backends/test/suite/generate_markdown_summary.py <path to test report CSV file> |
| 62 | +# Markdown is written to stdout. |
| 63 | +# |
| 64 | + |
| 65 | + |
| 66 | +def aggregate_results(json_path: str) -> AggregatedSummary: |
| 67 | + with open(json_path) as f: |
| 68 | + data = json.load(f) |
| 69 | + |
| 70 | + # Count results and prepare data |
| 71 | + counts = ResultCounts() |
| 72 | + failed_tests = [] |
| 73 | + counts_by_param = {} |
| 74 | + |
| 75 | + for test_data in data["tests"]: |
| 76 | + result_meta = test_data.get("metadata") |
| 77 | + if result_meta: |
| 78 | + for subtest_meta in result_meta["subtests"]: |
| 79 | + result = subtest_meta["Result"] |
| 80 | + result_detail = subtest_meta.get("Result Detail") or "" |
| 81 | + |
| 82 | + counts.add_row(result, result_detail) |
| 83 | + |
| 84 | + params = subtest_meta["Params"] |
| 85 | + if params: |
| 86 | + if params not in counts_by_param: |
| 87 | + counts_by_param[params] = ResultCounts() |
| 88 | + counts_by_param[params].add_row(result, result_detail) |
| 89 | + |
| 90 | + if result.lower() == "fail": |
| 91 | + failed_tests.append(subtest_meta) |
| 92 | + |
| 93 | + return AggregatedSummary( |
| 94 | + counts=counts, |
| 95 | + failed_tests=failed_tests, |
| 96 | + counts_by_params=counts_by_param, |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def escape_for_markdown(text: str) -> str: |
| 101 | + """ |
| 102 | + Modify a string to properly display in a markdown table cell. |
| 103 | + """ |
| 104 | + if not text: |
| 105 | + return text |
| 106 | + |
| 107 | + # Replace newlines with <br /> tags |
| 108 | + escaped = text.replace("\n", "<br />") |
| 109 | + |
| 110 | + # Escape backslashes. |
| 111 | + escaped = escaped.replace("\\", "\\\\") |
| 112 | + |
| 113 | + # Escape pipe characters that would break table structure |
| 114 | + escaped = escaped.replace("|", "\\|") |
| 115 | + |
| 116 | + return escaped |
| 117 | + |
| 118 | + |
| 119 | +def generate_markdown(json_path: str, exit_code: int = 0): # noqa (C901) |
| 120 | + # Print warning if exit code is non-zero |
| 121 | + if exit_code != 0: |
| 122 | + print("> [!WARNING]") |
| 123 | + print( |
| 124 | + f"> Exit code {exit_code} was non-zero. Test process may have crashed. Check the job logs for more information.\n" |
| 125 | + ) |
| 126 | + |
| 127 | + results = aggregate_results(json_path) |
| 128 | + |
| 129 | + # Generate Summary section |
| 130 | + print("# Summary\n") |
| 131 | + total_excluding_skips = results.counts.passes + results.counts.fails |
| 132 | + pass_fraction = results.counts.passes / total_excluding_skips |
| 133 | + fail_fraction = results.counts.fails / total_excluding_skips |
| 134 | + print( |
| 135 | + f"- **Pass**: {results.counts.passes}/{total_excluding_skips} ({pass_fraction*100:.2f}%)" |
| 136 | + ) |
| 137 | + print( |
| 138 | + f"- **Fail**: {results.counts.fails}/{total_excluding_skips} ({fail_fraction*100:.2f}%)" |
| 139 | + ) |
| 140 | + print(f"- **Skip**: {results.counts.skips}") |
| 141 | + |
| 142 | + if results.counts_by_params: |
| 143 | + print("\n## Results by Parameters\n") |
| 144 | + |
| 145 | + # Extract all unique parameter keys from the JSON strings |
| 146 | + all_param_keys = set() |
| 147 | + parsed_params = {} |
| 148 | + |
| 149 | + for params_str in results.counts_by_params.keys(): |
| 150 | + # Parse the JSON string (it's a string representation of a dict) |
| 151 | + params_dict = json.loads(params_str) |
| 152 | + parsed_params[params_str] = params_dict |
| 153 | + all_param_keys.update(params_dict.keys()) |
| 154 | + |
| 155 | + if parsed_params and len(parsed_params) > 1: |
| 156 | + # Sort parameter keys for consistent column ordering |
| 157 | + sorted_param_keys = sorted(all_param_keys) |
| 158 | + |
| 159 | + # Create table header |
| 160 | + header_cols = sorted_param_keys + ["Pass", "Fail", "Skip", "Pass %"] |
| 161 | + print("| " + " | ".join(header_cols) + " |") |
| 162 | + print("|" + "|".join(["---"] * len(header_cols)) + "|") |
| 163 | + |
| 164 | + # Create table rows |
| 165 | + for params_str, counts in results.counts_by_params.items(): |
| 166 | + if params_str in parsed_params: |
| 167 | + params_dict = parsed_params[params_str] |
| 168 | + row_values = [] |
| 169 | + |
| 170 | + # Add parameter values |
| 171 | + for key in sorted_param_keys: |
| 172 | + value = params_dict.get(key, "") |
| 173 | + row_values.append(str(value)) |
| 174 | + |
| 175 | + pass_fraction = counts.passes / (counts.passes + counts.fails) |
| 176 | + |
| 177 | + # Add count values |
| 178 | + row_values.extend( |
| 179 | + [ |
| 180 | + str(counts.passes), |
| 181 | + str(counts.fails), |
| 182 | + str(counts.skips), |
| 183 | + f"{pass_fraction*100:.2f}%", |
| 184 | + ] |
| 185 | + ) |
| 186 | + |
| 187 | + print("| " + " | ".join(row_values) + " |") |
| 188 | + |
| 189 | + print() |
| 190 | + |
| 191 | + print("## Failure Breakdown:") |
| 192 | + total_rows_with_result_detail = sum(results.counts.by_detail.values()) |
| 193 | + for detail, count in sorted(results.counts.by_detail.items()): |
| 194 | + print(f"- **{detail}**: {count}/{total_rows_with_result_detail}") |
| 195 | + |
| 196 | + # Generate Failed Tests section |
| 197 | + print("# Failed Tests\n") |
| 198 | + if results.failed_tests: |
| 199 | + header = build_header(results.failed_tests) |
| 200 | + |
| 201 | + escaped_header = [escape_for_markdown(col) for col in header.keys()] |
| 202 | + print("| " + " | ".join(escaped_header) + " |") |
| 203 | + print("|" + "|".join(["---"] * len(escaped_header)) + "|") |
| 204 | + for rec in results.failed_tests: |
| 205 | + row = build_row(rec, header) |
| 206 | + print("| " + " | ".join(row) + " |") |
| 207 | + else: |
| 208 | + print("No failed tests.\n") |
| 209 | + |
| 210 | + |
| 211 | +def build_header(data) -> dict[str, int]: |
| 212 | + """ |
| 213 | + Find the union of all keys and return a dict of header keys and indices. Try to preserve |
| 214 | + ordering as much as possible. |
| 215 | + """ |
| 216 | + |
| 217 | + keys = max(data, key=len) |
| 218 | + |
| 219 | + header = { |
| 220 | + k:i for (i,k) in enumerate(keys) |
| 221 | + } |
| 222 | + |
| 223 | + for rec in data: |
| 224 | + keys = set(rec.keys()) |
| 225 | + for k in keys: |
| 226 | + if k not in header: |
| 227 | + header[k] = len(header) |
| 228 | + |
| 229 | + return header |
| 230 | + |
| 231 | +def build_row(rec, header: dict[str, int]) -> list[str]: |
| 232 | + row = [""] * len(header) |
| 233 | + for k, v in rec.items(): |
| 234 | + row[header[k]] = escape_for_markdown(str(v)) |
| 235 | + return row |
| 236 | + |
| 237 | + |
| 238 | +def main(): |
| 239 | + parser = argparse.ArgumentParser( |
| 240 | + description="Generate a Markdown representation of a test report." |
| 241 | + ) |
| 242 | + parser.add_argument("csv_path", help="Path to the test report CSV file.") |
| 243 | + parser.add_argument( |
| 244 | + "--exit-code", type=int, default=0, help="Exit code from the test process." |
| 245 | + ) |
| 246 | + args = parser.parse_args() |
| 247 | + generate_markdown(args.csv_path, args.exit_code) |
| 248 | + |
| 249 | + |
| 250 | +if __name__ == "__main__": |
| 251 | + main() |
0 commit comments