more features

This commit is contained in:
Yatsishin Ilya 2020-04-16 18:04:31 +03:00
parent f2136bc286
commit a312cb6b0d

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import sys
import lxml.etree as etree import lxml.etree as etree
import json import json
import argparse
def export_testcases_json(report, path): def export_testcases_json(report, path):
with open(os.path.join(path, "cases.jer"), "w") as testcases_file: with open(os.path.join(path, "cases.jer"), "w") as testcases_file:
@ -11,7 +11,8 @@ def export_testcases_json(report, path):
for testcase in testsuite: for testcase in testsuite:
row = {} row = {}
row["hostname"] = testsuite.get("hostname") row["hostname"] = testsuite.get("hostname")
row["hostname"] = testsuite.get("hostname") row["suite"] = testsuite.get("name")
row["suite_duration"] = testsuite.get("time")
row["timestamp"] = testsuite.get("timestamp") row["timestamp"] = testsuite.get("timestamp")
row["testname"] = testcase.get("name") row["testname"] = testcase.get("name")
row["classname"] = testcase.get("classname") row["classname"] = testcase.get("classname")
@ -24,12 +25,13 @@ def export_testcases_json(report, path):
if el.tag == "system-out": if el.tag == "system-out":
row["stdout"] = el.text row["stdout"] = el.text
json.dump(row, testcases_file) json.dump(row, testcases_file)
testcases_file.write("\n")
def export_testsuites_json(report, path): def export_testsuites_json(report, path):
with open(os.path.join(path, "suites.jer"), "w") as testsuites_file: with open(os.path.join(path, "suites.jer"), "w") as testsuites_file:
for testsuite in report.getroot(): for testsuite in report.getroot():
row = {} row = {}
row["name"] = testsuite.get("name") row["suite"] = testsuite.get("name")
row["errors"] = testsuite.get("errors") row["errors"] = testsuite.get("errors")
row["failures"] = testsuite.get("failures") row["failures"] = testsuite.get("failures")
row["hostname"] = testsuite.get("hostname") row["hostname"] = testsuite.get("hostname")
@ -37,9 +39,10 @@ def export_testsuites_json(report, path):
row["duration"] = testsuite.get("time") row["duration"] = testsuite.get("time")
row["timestamp"] = testsuite.get("timestamp") row["timestamp"] = testsuite.get("timestamp")
json.dump(row, testsuites_file) json.dump(row, testsuites_file)
testsuites_file.write("\n")
def _convert_junit_to_html(junit_path, result_path): def _convert_junit_to_html(junit_path, result_path, export_cases, export_suites):
with open(os.path.join(os.path.dirname(__file__), "junit-noframes.xsl")) as xslt_file: with open(os.path.join(os.path.dirname(__file__), "junit-noframes.xsl")) as xslt_file:
junit_to_html_xslt = etree.parse(xslt_file) junit_to_html_xslt = etree.parse(xslt_file)
if not os.path.exists(result_path): if not os.path.exists(result_path):
@ -48,8 +51,10 @@ def _convert_junit_to_html(junit_path, result_path):
with open(junit_path) as junit_file: with open(junit_path) as junit_file:
junit_xml = etree.parse(junit_file) junit_xml = etree.parse(junit_file)
export_testsuites_json(junit_xml, result_path) if export_suites:
export_testcases_json(junit_xml, result_path) export_testsuites_json(junit_xml, result_path)
if export_cases:
export_testcases_json(junit_xml, result_path)
transform = etree.XSLT(junit_to_html_xslt) transform = etree.XSLT(junit_to_html_xslt)
html = etree.tostring(transform(junit_xml), encoding="utf-8") html = etree.tostring(transform(junit_xml), encoding="utf-8")
@ -57,8 +62,19 @@ def _convert_junit_to_html(junit_path, result_path):
html_file.write(html) html_file.write(html)
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) < 3:
raise "Insufficient arguments: junit.xml result_path ", level parser = argparse.ArgumentParser(description='Convert JUnit XML.')
junit_path, result_path = sys.argv[1] , sys.argv[2] parser.add_argument('junit', help='path to junit.xml report')
print "junit_path: {}, result_path: {}".format(junit_path, result_path) parser.add_argument('result_dir', nargs='?', help='directory for result files. Default to junit.xml directory')
_convert_junit_to_html(junit_path, result_path) parser.add_argument('--export-cases', help='Export JSONEachRow result for testcases to upload in CI', action='store_true')
parser.add_argument('--export-suites', help='Export JSONEachRow result for testsuites to upload in CI', action='store_true')
args = parser.parse_args()
junit_path = args.junit
if args.result_dir:
result_path = args.result_dir
else:
result_path = os.path.dirname(junit_path)
print "junit_path: {}, result_path: {}, export cases:{}, export suites: {}".format(junit_path, result_path, args.export_cases, args.export_suites)
_convert_junit_to_html(junit_path, result_path, args.export_cases, args.export_suites)