Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and features on the JUnit Python conversion script #594

Merged
merged 1 commit into from
Nov 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 54 additions & 39 deletions auto/unity_to_junit.py → auto/stylize_as_junit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
#! python3
# ==========================================
# Fork from Unity Project - A Test Framework for C
# Pull request on Gerrit in progress, the objective of this file is to be deleted when official Unity deliveries
# include that modification
# Copyright (c) 2015 Alexander Mueller / [email protected]
# [Released under MIT License. Please refer to license.txt for details]
# ==========================================
import sys
import os
from glob import glob
import argparse

from pyparsing import *
from junit_xml import TestSuite, TestCase
Expand All @@ -14,6 +23,7 @@ def __init__(self):
self.ignored = 0
self.targets = 0
self.root = None
self.output = None
self.test_suites = dict()

def run(self):
Expand All @@ -37,15 +47,18 @@ def run(self):
entry = entry_one | entry_two

delimiter = Literal(':').suppress()
tc_result_line = Group(entry.setResultsName('tc_file_name') + delimiter + entry.setResultsName(
'tc_line_nr') + delimiter + entry.setResultsName('tc_name') + delimiter + entry.setResultsName(
'tc_status') + Optional(
delimiter + entry.setResultsName('tc_msg'))).setResultsName("tc_line")
# Format of a result line is `[file_name]:line:test_name:RESULT[:msg]`
tc_result_line = Group(ZeroOrMore(entry.setResultsName('tc_file_name'))
+ delimiter + entry.setResultsName('tc_line_nr')
+ delimiter + entry.setResultsName('tc_name')
+ delimiter + entry.setResultsName('tc_status') +
Optional(delimiter + entry.setResultsName('tc_msg'))).setResultsName("tc_line")

eol = LineEnd().suppress()
sol = LineStart().suppress()
blank_line = sol + eol

# Format of the summary line is `# Tests # Failures # Ignored`
tc_summary_line = Group(Word(nums).setResultsName("num_of_tests") + "Tests" + Word(nums).setResultsName(
"num_of_fail") + "Failures" + Word(nums).setResultsName("num_of_ignore") + "Ignored").setResultsName(
"tc_summary")
Expand All @@ -67,7 +80,10 @@ def run(self):
tmp_tc_line = r['tc_line']

# get only the file name which will be used as the classname
file_name = tmp_tc_line['tc_file_name'].split('\\').pop().split('/').pop().rsplit('.', 1)[0]
if 'tc_file_name' in tmp_tc_line:
file_name = tmp_tc_line['tc_file_name'].split('\\').pop().split('/').pop().rsplit('.', 1)[0]
else:
file_name = result_file.strip("./")
tmp_tc = TestCase(name=tmp_tc_line['tc_name'], classname=file_name)
if 'tc_status' in tmp_tc_line:
if str(tmp_tc_line['tc_status']) == 'IGNORE':
Expand Down Expand Up @@ -96,7 +112,7 @@ def run(self):
for suite_name in self.test_suites:
ts.append(TestSuite(suite_name, self.test_suites[suite_name]))

with open('result.xml', 'w') as f:
with open(self.output, 'w') as f:
TestSuite.to_file(f, ts, prettyprint='True', encoding='utf-8')

return self.report
Expand All @@ -107,40 +123,39 @@ def set_targets(self, target_array):
def set_root_path(self, path):
self.root = path

@staticmethod
def usage(err_msg=None):
print("\nERROR: ")
if err_msg:
print(err_msg)
print("\nUsage: unity_test_summary.py result_file_directory/ root_path/")
print(" result_file_directory - The location of your results files.")
print(" Defaults to current directory if not specified.")
print(" Should end in / if specified.")
print(" root_path - Helpful for producing more verbose output if using relative paths.")
sys.exit(1)
def set_output(self, output):
self.output = output


if __name__ == '__main__':
uts = UnityTestSummary()
try:
# look in the specified or current directory for result files
if len(sys.argv) > 1:
targets_dir = sys.argv[1]
else:
targets_dir = './'
targets = list(map(lambda x: x.replace('\\', '/'), glob(targets_dir + '*.test*')))
if len(targets) == 0:
raise Exception("No *.testpass or *.testfail files found in '%s'" % targets_dir)
uts.set_targets(targets)

# set the root path
if len(sys.argv) > 2:
root_path = sys.argv[2]
else:
root_path = os.path.split(__file__)[0]
uts.set_root_path(root_path)

# run the summarizer
print(uts.run())
except Exception as e:
UnityTestSummary.usage(e)
parser = argparse.ArgumentParser(description=
"""Takes as input the collection of *.testpass and *.testfail result
files, and converts them to a JUnit formatted XML.""")
parser.add_argument('targets_dir', metavar='result_file_directory',
type=str, nargs='?', default='./',
help="""The location of your results files.
Defaults to current directory if not specified.""")
parser.add_argument('root_path', nargs='?',
default='os.path.split(__file__)[0]',
help="""Helpful for producing more verbose output if
using relative paths.""")
parser.add_argument('--output', '-o', type=str, default="result.xml",
help="""The name of the JUnit-formatted file (XML).""")
args = parser.parse_args()

if args.targets_dir[-1] != '/':
args.targets_dir+='/'
targets = list(map(lambda x: x.replace('\\', '/'), glob(args.targets_dir + '*.test*')))
if len(targets) == 0:
raise Exception("No *.testpass or *.testfail files found in '%s'" % args.targets_dir)
uts.set_targets(targets)

# set the root path
uts.set_root_path(args.root_path)

# set output
uts.set_output(args.output)

# run the summarizer
print(uts.run())