Skip to content

Commit

Permalink
added junit xml output for testcases (#117)
Browse files Browse the repository at this point in the history
lint + pydoc



lint



lint

Signed-off-by: Tullio Sebastiani <[email protected]>
  • Loading branch information
tsebastiani committed Aug 13, 2024
1 parent 1e94edb commit 1051a31
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/krkn_lib/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
get_random_string,
get_yaml_item_value,
is_host_reachable,
get_junit_test_case,
)


Expand Down Expand Up @@ -323,3 +324,66 @@ def test_is_host_reachable(self):
self.assertFalse(
is_host_reachable(f"{get_random_string(10)}.com", 12345)
)

def test_get_junit_test_case(self):
test_suite_name = "krkn-test"
test_case_description_success = (
"success [test-case] krkn-lib test case"
)
test_case_description_failure = (
"failure [test-case] krkn-lib test case"
)
test_stdout = "KRKN STDOUT"
test_version = "OCP 4.16"
time = 10
success_output = (
f'<testsuite name="{test_suite_name}" tests="1" skipped="0" failures="0" time="10">' # NOQA
f'<property name="TestVersion" value="{test_version}" />'
f'<testcase name="{test_case_description_success}" time="{time}" />' # NOQA
f"</testsuite>"
)

success_output_not_test_version = (
f'<testsuite name="{test_suite_name}" tests="1" skipped="0" failures="0" time="10">' # NOQA
f'<testcase name="{test_case_description_success}" time="{time}" />' # NOQA
f"</testsuite>"
)

failure_output = (
f'<testsuite name="{test_suite_name}" tests="1" skipped="0" failures="1" time="10">' # NOQA
f'<property name="TestVersion" value="{test_version}" />'
f'<testcase name="{test_case_description_failure}" time="{time}">'
f'<failure message="">{test_stdout}</failure>'
f"</testcase>"
f"</testsuite>"
)
success_test = get_junit_test_case(
True,
time,
test_suite_name,
test_case_description_success,
test_stdout,
test_version,
)
success_test_not_test_version = get_junit_test_case(
True,
time,
test_suite_name,
test_case_description_success,
test_stdout,
)

failure_test = get_junit_test_case(
False,
time,
test_suite_name,
test_case_description_failure,
test_stdout,
test_version,
)

self.assertEqual(success_output, success_test)
self.assertEqual(
success_output_not_test_version, success_test_not_test_version
)
self.assertEqual(failure_output, failure_test)
46 changes: 46 additions & 0 deletions src/krkn_lib/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from base64io import Base64IO
from dateutil import parser
from dateutil.parser import ParserError
import xml.etree.cElementTree as ET


def decode_base64_file(source_filename: str, destination_filename: str):
Expand Down Expand Up @@ -380,3 +381,48 @@ def is_host_reachable(host: str, port: int, timeout: int = 2) -> bool:
return True
except OSError:
return False


def get_junit_test_case(
success: bool,
time: int,
test_suite_name: str,
test_case_description: str,
test_stdout: str = "",
test_version: str = None,
) -> str:
"""
Creates an XML compatible with sippy to track regressions
on OCP tests.
:param success: if true will print a success test case,
otherwise a failure test case
:param time: sets the duration in seconds of the testsuite
:param test_suite_name: sets the name of the test suite
:param test_case_description: sets the description of
the testcase, it has to contain tags that map the test case
to the monitored component on sippy like [sig-etcd]
or others
:param test_stdout: if a test failes the stdout of the krkn-run
is attached to the testcase element in the XML
:param test_version: sets an optional property to the testsuite
containing the version on the test
:return: the XML string to be written in the junit xml file
"""

root = ET.Element("testsuite")
root.attrib["name"] = test_suite_name
root.attrib["tests"] = "1"
root.attrib["skipped"] = "0"
root.attrib["failures"] = "0" if success else "1"
root.attrib["time"] = f"{time}"
if test_version:
ET.SubElement(root, "property", name="TestVersion", value=test_version)

test_case = ET.SubElement(
root, "testcase", name=test_case_description, time=f"{time}"
)
if not success:
ET.SubElement(test_case, "failure", message="").text = test_stdout

return ET.tostring(root, encoding="utf-8").decode("UTF-8")

0 comments on commit 1051a31

Please sign in to comment.