-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_summary_parser_csv.py
More file actions
executable file
·84 lines (69 loc) · 2.98 KB
/
test_summary_parser_csv.py
File metadata and controls
executable file
·84 lines (69 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import re
import os
import csv
import sys
from pathlib import Path
from typing import Dict, List, Optional
def parse_test_summary(line: str) -> Optional[Dict[str, int]]:
"""Extract test summary from a line if it matches the pattern"""
pattern = r"Tests\s*run:\s*(\d+),\s*Failures:\s*(\d+),\s*Errors:\s*(\d+),\s*Skipped:\s*(\d+)"
match = re.search(pattern, line)
if match:
return {
"tests_run": int(match.group(1)),
"failures": int(match.group(2)),
"errors": int(match.group(3)),
"skipped": int(match.group(4))
}
return None
def process_file(file_path: Path) -> List[Dict[str, str]]:
"""Process a file and return all test summaries with source file path"""
summaries = []
try:
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
if summary := parse_test_summary(line):
summary["source_file"] = str(file_path)
summaries.append(summary)
except UnicodeDecodeError:
with open(file_path, 'r', encoding='latin-1') as f:
for line in f:
if summary := parse_test_summary(line):
summary["source_file"] = str(file_path)
summaries.append(summary)
return summaries
def find_test_summaries_to_csv(directory: str, output_file: str = "test_results.csv") -> None:
"""
Search directory recursively for test summaries.
Save results in CSV format with one row per test run.
"""
root_path = Path(directory)
all_summaries = []
# Process all relevant files
for ext in ('*.log', '*.txt'):
for file_path in root_path.rglob(ext):
all_summaries.extend(process_file(file_path))
# Save results to CSV
if all_summaries:
fieldnames = ["source_file", "tests_run",
"failures", "errors", "skipped"]
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(all_summaries)
total_summaries = len(all_summaries)
print(f"Found {total_summaries} test summaries. Saved to {output_file}")
else:
print("No test summaries found in the specified directory.")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(
description="Parse test execution logs and save to CSV")
parser.add_argument("directory", help="Directory to search for log files")
parser.add_argument("-o", "--output", default="test_results.csv",
help="Output CSV file path (default: test_results.csv)")
args = parser.parse_args()
if not os.path.isdir(args.directory):
print(f"Error: Directory not found - {args.directory}")
sys.exit(1)
find_test_summaries_to_csv(args.directory, args.output)