Skip to content

Commit

Permalink
Added --report-junit to options. So CI can be enriched with Junit res…
Browse files Browse the repository at this point in the history
…ults and plots
  • Loading branch information
PrzemekWirkus committed May 21, 2015
1 parent 75a4cf7 commit 8d7a14f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
32 changes: 28 additions & 4 deletions mbed_greentea/mbed_greentea_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
from mbed_test_api import TEST_RESULTS
from cmake_handlers import load_ctest_testsuite
from cmake_handlers import list_binaries_for_targets
from mbed_report_api import exporter_junit
from mbed_target_info import get_mbed_clasic_target_info
from mbed_target_info import get_mbed_supported_test


try:
import mbed_lstools
import mbed_host_tests
Expand Down Expand Up @@ -103,6 +105,10 @@ def main():
dest='digest_source',
help='Redirect input from where test suite should take console input. You can use stdin or file name to get test case console output')

parser.add_option('', '--report-junit',
dest='report_junit_file_name',
help='You can log test suite results in form of JUnit compliant XML report')

parser.add_option('-V', '--verbose-test-result',
dest='verbose_test_result_only',
default=False,
Expand Down Expand Up @@ -141,6 +147,8 @@ def main():
print "mbed-ls: detecting connected mbed-enabled devices... %s"% ("no devices detected" if not len(mbeds_list) else "")
list_of_targets = opts.list_of_targets.split(',') if opts.list_of_targets is not None else None

test_report = {} # Test report used to export to Junit, HTML etc...

for mut in mbeds_list:
print "mbed-ls: detected %s, console at: %s, mounted at: %s"% (mut['platform_name'],
mut['serial_port'],
Expand All @@ -155,7 +163,6 @@ def main():
for yotta_target in mut_info['yotta_targets']:
yotta_target_name = yotta_target['yotta_target']
yotta_target_toolchain = yotta_target['mbed_toolchain']
print "\tgot yotta target '%s'"% (yotta_target_name)

if opts.verbose_test_configuration_only:
continue
Expand All @@ -179,7 +186,7 @@ def main():
ctest_test_list = load_ctest_testsuite(os.path.join('.', 'build', yotta_target_name),
binary_type=binary_type)

print "mbedgt: running tests..."
print "mbedgt: running tests for '%s' target" % yotta_target_name
for test_bin, image_path in ctest_test_list.iteritems():
test_result = 'SKIPPED'
# Skip test not mentionned in -n option
Expand All @@ -204,8 +211,25 @@ def main():
verbose=verbose)
single_test_result, single_test_output, single_testduration, single_timeout = host_test_result
test_result = single_test_result
print "\ttest '%s' %s"% (test_bin, '.' * (70 - len(test_bin))),
print "%s"% (test_result)

# Update report for optional reporting feature
test_name = test_bin.lower()
if yotta_target_name not in test_report:
test_report[yotta_target_name] = {}
if test_name not in test_report[yotta_target_name]:
test_report[yotta_target_name][test_name] = {}

test_report[yotta_target_name][test_name]['single_test_result'] = single_test_result
test_report[yotta_target_name][test_name]['single_test_output'] = single_test_output
test_report[yotta_target_name][test_name]['elapsed_time'] = single_testduration

print "\ttest '%s' %s"% (test_bin, '.' * (70 - len(test_bin))),
print " %s in %.2f sec"% (test_result, single_testduration)

if opts.report_junit_file_name:
junit_report = exporter_junit(test_report)
with open(opts.report_junit_file_name, 'w') as f:
f.write(junit_report)

if opts.verbose_test_configuration_only:
print
Expand Down
52 changes: 52 additions & 0 deletions mbed_greentea/mbed_report_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
mbed SDK
Copyright (c) 2011-2015 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Przemyslaw Wirkus <[email protected]>
"""

from junit_xml import TestSuite, TestCase


def exporter_junit(test_result_ext, test_suite_properties=None):
""" Export test results in JUnit XML compliant format
"""
test_suites = []
test_cases = []

targets = sorted(test_result_ext.keys())
for target in targets:
test_cases = []
tests = sorted(test_result_ext[target].keys())
for test in tests:
test_results = test_result_ext[target][test]
classname = 'test.%s.%s' % (target, test)
elapsed_sec = test_results['elapsed_time']
_stdout = test_results['single_test_output']
_stderr = ''
# Test case
tc = TestCase(test, classname, elapsed_sec, _stdout, _stderr)
# Test case extra failure / error info
if test_results['single_test_result'] == 'FAIL':
message = test_results['single_test_result']
tc.add_failure_info(message, _stdout)
elif test_results['single_test_result'] != 'OK':
message = test_results['single_test_result']
tc.add_error_info(message, _stdout)

test_cases.append(tc)
ts = TestSuite("test.suite.%s" % target, test_cases)
test_suites.append(ts)
return TestSuite.to_xml_string(test_suites)

0 comments on commit 8d7a14f

Please sign in to comment.