-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_failed_tests_from_xml.py
66 lines (53 loc) · 1.96 KB
/
extract_failed_tests_from_xml.py
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
import xml.etree.ElementTree as ET
import pyperclip
import os
import pathlib
def get_xml_files_from_dirs(dirs):
xml_files = []
for dir_path in dirs:
expanded_dir_path = os.path.expanduser(dir_path)
if not os.path.exists(expanded_dir_path):
print(f"Directory not found: {expanded_dir_path}")
continue
for root, _, files in os.walk(expanded_dir_path):
for file in files:
if "report" in file.lower() and file.endswith(".xml"):
xml_files.append(os.path.join(root, file))
return xml_files
# Load the list of XML file paths and directories
dirs = [
str(pathlib.Path.home() / "Downloads"),
str(pathlib.Path.home() / "results/reports"),
]
file_paths = get_xml_files_from_dirs(dirs) + [
# Add additional file paths here as strings
]
# Extract the names of all failed test cases from multiple files
failed_test_cases = []
for file_path in file_paths:
if not os.path.exists(file_path):
print(f"File not found: {file_path}")
continue
# Skip empty files
if os.path.getsize(file_path) == 0:
print(f"Empty file skipped: {file_path}")
continue
# Parse the XML file
try:
tree = ET.parse(file_path)
root = tree.getroot()
except ET.ParseError:
print(f"Parse error in file: {file_path}")
continue
# Extract the names of all failed test cases
for test_case in root.iter("test-case"):
if test_case.get("result") == "Failed":
failed_test_cases.append(test_case.get("name"))
# Count the number of failed test cases
num_failed_test_cases = len(failed_test_cases)
# Format the list as requested
formatted_failed_test_cases = "(" + "|".join(failed_test_cases) + ")"
print("Number of failed test cases:\n", num_failed_test_cases)
print("Failed test cases:\n", formatted_failed_test_cases)
# Copy the formatted failed test cases to the clipboard
pyperclip.copy(formatted_failed_test_cases)