blob: 82b2f139ad4984c6bd00a2374a40f448d98032f8 [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
20import os
21import math
22from datetime import datetime
23import xml.etree.ElementTree as et
24from . import log, suite
25
26def trial_to_junit_write(trial, junit_path):
27 elements = et.ElementTree(element=trial_to_junit(trial))
28 elements.write(junit_path)
29
30def trial_to_junit(trial):
31 testsuites = et.Element('testsuites')
32 for suite in trial.suites:
33 testsuite = suite_to_junit(suite)
34 testsuites.append(testsuite)
35 return testsuites
36
37def suite_to_junit(suite):
38 testsuite = et.Element('testsuite')
39 testsuite.set('name', suite.name())
40 testsuite.set('hostname', 'localhost')
Neels Hofmeyrf8e61862017-06-06 23:08:07 +020041 if suite.start_timestamp:
42 testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat())
43 testsuite.set('time', str(math.ceil(suite.duration)))
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020044 testsuite.set('tests', str(len(suite.tests)))
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020045 testsuite.set('failures', str(suite.count_test_results()[2]))
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020046 for test in suite.tests:
47 testcase = test_to_junit(test)
48 testsuite.append(testcase)
49 return testsuite
50
51def test_to_junit(test):
52 testcase = et.Element('testcase')
53 testcase.set('name', test.name())
54 testcase.set('time', str(math.ceil(test.duration)))
55 if test.status == suite.Test.SKIP:
56 skip = et.SubElement(testcase, 'skipped')
57 elif test.status == suite.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020058 failure = et.SubElement(testcase, 'failure')
59 failure.set('type', test.fail_type or 'failure')
60 failure.text = test.fail_message
61 if test.fail_tb:
62 system_err = et.SubElement(testcase, 'system-err')
63 system_err.text = test.fail_tb
Neels Hofmeyrf8e61862017-06-06 23:08:07 +020064 elif test.status != suite.Test.PASS:
65 error = et.SubElement(testcase, 'error')
66 error.text = 'could not run'
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020067 return testcase
68
69def trial_to_text(trial):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020070 suite_failures = []
71 count_fail = 0
72 count_pass = 0
73 for suite in trial.suites:
74 if suite.passed():
75 count_pass += 1
76 else:
77 count_fail += 1
78 suite_failures.append(suite_to_text(suite))
79
80 summary = ['%s: %s' % (trial.name(), trial.status)]
81 if count_fail:
82 summary.append('%d suites failed' % count_fail)
83 if count_pass:
84 summary.append('%d suites passed' % count_pass)
85 msg = [', '.join(summary)]
86 msg.extend(suite_failures)
87 return '\n'.join(msg)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020088
89def suite_to_text(suite):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020090 if not suite.tests:
91 return 'no tests were run.'
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020092
93 passed, skipped, failed = suite.count_test_results()
94 details = []
95 if failed:
96 details.append('fail: %d' % failed)
97 if passed:
98 details.append('pass: %d' % passed)
99 if skipped:
100 details.append('skip: %d' % skipped)
101 msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))]
102 msgs.extend([test_to_text(t) for t in suite.tests])
103 return '\n '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200104
105def test_to_text(test):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200106 msgs = ['%s: %s' % (test.status, test.name())]
107 if test.start_timestamp:
108 msgs.append('(%.1f sec)' % test.duration)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200109 if test.status == suite.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200110 msgs.append('%s: %s' % (test.fail_type, test.fail_message))
111 return ' '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200112
113# vim: expandtab tabstop=4 shiftwidth=4