Created
May 8, 2020 14:20
-
-
Save zeegin/e03a76308fb67aa96e2f084ccbeef8c5 to your computer and use it in GitHub Desktop.
Script is printing junit result to console and stop the process if error or fails was found
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# use as: | |
# python -u junit_to_console.py print junit.xml | |
import argparse | |
from junitparser import JUnitXml | |
from tabulate import tabulate | |
def print_result(paths): | |
result = JUnitXml() | |
for path in args.paths: | |
result += JUnitXml.fromfile(path) | |
result.update_statistics() | |
print('\r\n') | |
print(f'Tests: {result.tests}\t' | |
f'Errors: {result.errors}\t' | |
f'Failures: {result.failures}\t' | |
f'Skipped: {result.skipped}\t' | |
f'Time: {result.time}\r\n') | |
data = [] | |
errors = [] | |
for suite in result: | |
data.append([suite.name, '', suite.time]) | |
for case in suite: | |
if case.result: | |
status = case.result._tag | |
errors.append(case) | |
else: | |
status = 'success' | |
data.append([f'> {case.name}', status, case.time]) | |
print(tabulate(data, headers=['Name', 'Status', 'Time'])) | |
print('\r\n') | |
for error in errors: | |
print('================================================================================') | |
print(f'{error.result._tag}\t{error.name}\r\n') | |
texts = [] | |
for inner_text in error.result._elem.itertext(): | |
part = inner_text.strip() | |
if part: | |
texts.append(part) | |
print(" ".join(texts)) | |
if (len(errors) > 0): | |
exit_code = 1 | |
else: | |
exit_code = 0 | |
exit(exit_code) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Junit to console.') | |
command_parser = parser.add_subparsers( | |
dest='command', | |
help='command' | |
) | |
# merge | |
print_parser = command_parser.add_parser( | |
'print', | |
help='Print Junit XML format reports.' | |
) | |
print_parser.add_argument( | |
'paths', | |
nargs='+', | |
help='Original XML path(s).' | |
) | |
args = parser.parse_args() | |
# entrypoint | |
if args.command == 'print': | |
print_result(args.paths) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment