From d5f613f5b05871e55aec605e4920a73f5806e64c Mon Sep 17 00:00:00 2001 From: Drew Reed Date: Fri, 23 Jul 2021 09:57:39 +0100 Subject: [PATCH 1/2] Added support for outputing results in junit format Added new --junit command line option to instruct the script to output the results in junit format for easy inclusion in CI systems Signed-off-by: Drew Reed --- parser.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/parser.py b/parser.py index 5ceda2d..73204ab 100755 --- a/parser.py +++ b/parser.py @@ -24,6 +24,13 @@ except ImportError: 'No yaml. You should install PyYAML/python3-yaml for configuration' ' file support...') +try: + from junit_xml import TestSuite, TestCase +except ImportError: + print( + 'No junit_xml. You should install junit_xml for junit output' + ' support...') + if 'yaml' in sys.modules: try: from yaml import CDumper as Dumper @@ -520,6 +527,45 @@ def gen_json(cross_check, filename): json.dump(cross_check, jsonfile, sort_keys=True, indent=2) +# Generate junit +def gen_junit(cross_check, filename): + logging.debug(f'Generate {filename}') + + testsuites = {} + + for result in cross_check: + testcase = TestCase( + result['name'] if result['name'] else result['sub set'], + (result['test set'] if result['test set'] else + result['set guid']) + "." + result['sub set'], + 0, + "Description: " + result['descr'] + + "\nSet GUID: " + result['set guid'] + + "\nGUID: " + result['guid'] + + "\nDevice Path: " + result['device path'] + + "\nStart Date: " + result['start date'] + + "\nStart Time: " + result['start time'] + + "\nRevision: " + result['revision'] + + "\nIteration: " + result['iteration'] + + "\nLog: " + result['log'], + "") + if result['result'] == 'FAILURE': + testcase.add_failure_info(result['result']) + elif result['result'] == 'SKIPPED': + testcase.add_skipped_info(result['result']) + elif result['result'] == 'DROPPED': + testcase.add_skipped_info(result['result']) + + group = result['group'] if result['group'] else result['test set'] + if group not in testsuites: + testsuites[group] = TestSuite(group) + + testsuites[group].test_cases.append(testcase) + + with open(filename, 'w') as file: + TestSuite.to_file(file, testsuites.values()) + + # Generate yaml def gen_yaml(cross_check, filename): assert('yaml' in sys.modules) @@ -876,6 +922,7 @@ if __name__ == '__main__': formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--csv', help='Output .csv filename') parser.add_argument('--json', help='Output .json filename') + parser.add_argument('--junit', help='Output .junit filename') parser.add_argument( '--md', help='Output .md filename', default='result.md') parser.add_argument( @@ -1039,6 +1086,10 @@ if __name__ == '__main__': if args.json is not None: gen_json(cross_check, args.json) + # Generate junit if requested + if args.junit is not None: + gen_junit(cross_check, args.junit) + # Generate yaml if requested if 'yaml' in args and args.yaml is not None: gen_yaml(cross_check, args.yaml) -- GitLab From 2dbcbd3b5771e2ec09d0d3a8beb3868265106e1e Mon Sep 17 00:00:00 2001 From: Drew Reed Date: Tue, 19 Jul 2022 08:30:45 +0000 Subject: [PATCH 2/2] Addressed review comments --- README.md | 8 ++++++++ parser.py | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7108f7f..00927c6 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ detection. Depending on your Linux distribution, this might be available as the `python3-packaging` package. The [python-jsonschema] module is required for configuration validation. See [Configuration file]. +The [junit-xml] module to allow junit format report generation. If you want to generate the pdf version of this documentation or convert markdown results to HTML, you need to install [pandoc]. See [Usage] and @@ -30,6 +31,7 @@ markdown results to HTML, you need to install [pandoc]. See [Usage] and [PyYAML]: https://github.com/yaml/pyyaml [packaging]: https://github.com/pypa/packaging +[junit-xml]: https://pypi.org/project/junit-xml [pandoc]: https://pandoc.org [python-jsonschema]: https://python-jsonschema.readthedocs.io @@ -63,6 +65,12 @@ The output filename can be specified with the `--md` option: $ ./parser.py --md out.md ... ``` +To generate a JUnit format report you can specify the output file with the `--junit` option: + +``` {.sh} +$ ./parser.py --junit report.xml ... +``` + An online help is available with the `-h` option. The generated `result md` can be easily converted to HTML using [pandoc] with: diff --git a/parser.py b/parser.py index 73204ab..cee31a3 100755 --- a/parser.py +++ b/parser.py @@ -529,6 +529,7 @@ def gen_json(cross_check, filename): # Generate junit def gen_junit(cross_check, filename): + assert('junit-xml' in sys.modules) logging.debug(f'Generate {filename}') testsuites = {} @@ -922,7 +923,11 @@ if __name__ == '__main__': formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--csv', help='Output .csv filename') parser.add_argument('--json', help='Output .json filename') - parser.add_argument('--junit', help='Output .junit filename') + + # junit-xml modules must of been loaded to enable the --junit option + if 'junit_xml' in sys.modules: + parser.add_argument('--junit', help='Output .junit filename') + parser.add_argument( '--md', help='Output .md filename', default='result.md') parser.add_argument( -- GitLab