aboutsummaryrefslogtreecommitdiffstats
path: root/.github/scripts/tests/junit_utils.py
blob: 232aaafa6b72c144df88036c09fda3ba2bdc21c2 (plain) (blame)
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
from xml.etree import ElementTree as ET


def get_or_create_properties(testcase):
    props = testcase.find("properties")
    if props is None:
        props = ET.Element("properties")
        testcase.append(props)
    return props


def add_junit_link_property(testcase, name, url):
    add_junit_property(testcase, f"url:{name}", url)


def add_junit_property(testcase, name, value):
    props = get_or_create_properties(testcase)
    props.append(ET.Element("property", dict(name=name, value=value)))


def add_junit_log_property(testcase, url):
    add_junit_link_property(testcase, "Log", url)


def get_property_value(testcase, name):
    props = testcase.find("properties")
    if props is None:
        return None

    for prop in props.findall("property"):
        if prop.attrib["name"] == name:
            return prop.attrib["value"]


def create_error_testsuite(testcases):
    n = str(len(testcases))
    root = ET.Element("testsuite", dict(tests=n, failures=n))
    root.extend(testcases)
    return ET.ElementTree(root)


def create_error_testcase(shardname, classname, name, log_fn=None, log_url=None):
    testcase = ET.Element("testcase", dict(classname=classname, name=name))
    add_junit_property(testcase, "shard", shardname)
    if log_url:
        add_junit_log_property(testcase, log_url)

    err = ET.Element("error", dict(type="error"))

    if log_fn:
        with open(log_fn, "rt") as fp:
            err.text = fp.read(4096)
    testcase.append(err)

    return testcase


def suite_case_iterator(root):
    for suite in root.findall("testsuite"):
        for case in suite.findall("testcase"):
            cls, method = case.attrib["classname"], case.attrib["name"]
            yield suite, case, cls, method