blob: 224cc463673e13c9255e38d2ed4d88f6c24ce50a [file] [log] [blame]
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +02001# osmo_gsm_tester: report: directory of binaries to be tested
2#
3# Copyright (C) 2016-2017 by sysmocom - s.f.m.c. GmbH
4#
5# Author: Pau Espin Pedrol <pespin@sysmocom.de>
6#
7# This program is free software: you can redistribute it and/or modify
Harald Welte27205342017-06-03 09:51:45 +02008# it under the terms of the GNU General Public License as
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +02009# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte27205342017-06-03 09:51:45 +020015# GNU General Public License for more details.
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020016#
Harald Welte27205342017-06-03 09:51:45 +020017# You should have received a copy of the GNU General Public License
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020018# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020020import math
21from datetime import datetime
22import xml.etree.ElementTree as et
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +010023from . import test
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020024
25def trial_to_junit_write(trial, junit_path):
26 elements = et.ElementTree(element=trial_to_junit(trial))
27 elements.write(junit_path)
28
29def trial_to_junit(trial):
30 testsuites = et.Element('testsuites')
31 for suite in trial.suites:
32 testsuite = suite_to_junit(suite)
33 testsuites.append(testsuite)
34 return testsuites
35
36def suite_to_junit(suite):
37 testsuite = et.Element('testsuite')
38 testsuite.set('name', suite.name())
39 testsuite.set('hostname', 'localhost')
Neels Hofmeyrf8e61862017-06-06 23:08:07 +020040 if suite.start_timestamp:
41 testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat())
42 testsuite.set('time', str(math.ceil(suite.duration)))
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020043 testsuite.set('tests', str(len(suite.tests)))
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020044 testsuite.set('failures', str(suite.count_test_results()[2]))
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020045 for test in suite.tests:
46 testcase = test_to_junit(test)
47 testsuite.append(testcase)
48 return testsuite
49
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010050def test_to_junit(t):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020051 testcase = et.Element('testcase')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010052 testcase.set('name', t.name())
53 testcase.set('time', str(math.ceil(t.duration)))
54 if t.status == test.Test.SKIP:
Holger Hans Peter Freytherd03acdf2018-09-23 18:06:48 +010055 et.SubElement(testcase, 'skipped')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010056 elif t.status == test.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020057 failure = et.SubElement(testcase, 'failure')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010058 failure.set('type', t.fail_type or 'failure')
59 failure.text = t.fail_message
60 if t.fail_tb:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020061 system_err = et.SubElement(testcase, 'system-err')
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +010062 system_err.text = t.fail_tb
63 elif t.status != test.Test.PASS:
Neels Hofmeyrf8e61862017-06-06 23:08:07 +020064 error = et.SubElement(testcase, 'error')
65 error.text = 'could not run'
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020066 return testcase
67
68def trial_to_text(trial):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020069 suite_failures = []
70 count_fail = 0
71 count_pass = 0
72 for suite in trial.suites:
73 if suite.passed():
74 count_pass += 1
75 else:
76 count_fail += 1
77 suite_failures.append(suite_to_text(suite))
78
79 summary = ['%s: %s' % (trial.name(), trial.status)]
80 if count_fail:
81 summary.append('%d suites failed' % count_fail)
82 if count_pass:
83 summary.append('%d suites passed' % count_pass)
84 msg = [', '.join(summary)]
85 msg.extend(suite_failures)
86 return '\n'.join(msg)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020087
88def suite_to_text(suite):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020089 if not suite.tests:
90 return 'no tests were run.'
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020091
92 passed, skipped, failed = suite.count_test_results()
93 details = []
94 if failed:
95 details.append('fail: %d' % failed)
96 if passed:
97 details.append('pass: %d' % passed)
98 if skipped:
99 details.append('skip: %d' % skipped)
100 msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))]
101 msgs.extend([test_to_text(t) for t in suite.tests])
102 return '\n '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200103
Pau Espin Pedrolfd5de3d2017-11-09 14:26:35 +0100104def test_to_text(t):
105 msgs = ['%s: %s' % (t.status, t.name())]
106 if t.start_timestamp:
107 msgs.append('(%.1f sec)' % t.duration)
108 if t.status == test.Test.FAIL:
109 msgs.append('%s: %s' % (t.fail_type, t.fail_message))
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200110 return ' '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200111
112# vim: expandtab tabstop=4 shiftwidth=4