pytorch
136 строк · 3.5 Кб
1import functools2import os3import warnings4
5
6try:7import lxml.etree8
9p = lxml.etree.XMLParser(huge_tree=True)10parse = functools.partial(lxml.etree.parse, parser=p)11except ImportError:12import xml.etree.ElementTree as ET13
14parse = ET.parse15warnings.warn(16"lxml was not found. `pip install lxml` to make this script run much faster"17)18
19
20def open_test_results(directory):21xmls = []22for root, _, files in os.walk(directory):23for file in files:24if file.endswith(".xml"):25tree = parse(f"{root}/{file}")26xmls.append(tree)27return xmls28
29
30def get_testcases(xmls):31testcases = []32for xml in xmls:33root = xml.getroot()34testcases.extend(list(root.iter("testcase")))35return testcases36
37
38def find(testcase, condition):39children = list(testcase.iter())40assert children[0] is testcase41children = children[1:]42return condition(children)43
44
45def skipped_test(testcase):46def condition(children):47return "skipped" in {child.tag for child in children}48
49return find(testcase, condition)50
51
52def passed_test(testcase):53def condition(children):54if len(children) == 0:55return True56tags = {child.tag for child in children}57return "skipped" not in tags and "failed" not in tags58
59return find(testcase, condition)60
61
62def key(testcase):63file = testcase.attrib.get("file", "UNKNOWN")64classname = testcase.attrib["classname"]65name = testcase.attrib["name"]66return "::".join([file, classname, name])67
68
69def get_passed_testcases(xmls):70testcases = get_testcases(xmls)71passed_testcases = [testcase for testcase in testcases if passed_test(testcase)]72return passed_testcases73
74
75def get_excluded_testcases(xmls):76testcases = get_testcases(xmls)77excluded_testcases = [t for t in testcases if excluded_testcase(t)]78return excluded_testcases79
80
81def excluded_testcase(testcase):82def condition(children):83for child in children:84if child.tag == "skipped":85if "Policy: we don't run" in child.attrib["message"]:86return True87return False88
89return find(testcase, condition)90
91
92def is_unexpected_success(testcase):93def condition(children):94for child in children:95if child.tag != "failure":96continue97is_unexpected_success = (98"unexpected success" in child.attrib["message"].lower()99)100if is_unexpected_success:101return True102return False103
104return find(testcase, condition)105
106
107MSG = "This test passed, maybe we can remove the skip from dynamo_test_failures.py"108
109
110def is_passing_skipped_test(testcase):111def condition(children):112for child in children:113if child.tag != "skipped":114continue115has_passing_skipped_test_msg = MSG in child.attrib["message"]116if has_passing_skipped_test_msg:117return True118return False119
120return find(testcase, condition)121
122
123# NB: not an unexpected success
124def is_failure(testcase):125def condition(children):126for child in children:127if child.tag != "failure":128continue129is_unexpected_success = (130"unexpected success" in child.attrib["message"].lower()131)132if not is_unexpected_success:133return True134return False135
136return find(testcase, condition)137