blob: ae63408c7767e592cacc0d18e180434e1f72a5a9 [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')
41 testsuite.set('timestamp', datetime.fromtimestamp(round(suite.start_timestamp)).isoformat())
42 testsuite.set('time', str(math.ceil(suite.duration)))
43 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
50def test_to_junit(test):
51 testcase = et.Element('testcase')
52 testcase.set('name', test.name())
53 testcase.set('time', str(math.ceil(test.duration)))
54 if test.status == suite.Test.SKIP:
55 skip = et.SubElement(testcase, 'skipped')
56 elif test.status == suite.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020057 failure = et.SubElement(testcase, 'failure')
58 failure.set('type', test.fail_type or 'failure')
59 failure.text = test.fail_message
60 if test.fail_tb:
61 system_err = et.SubElement(testcase, 'system-err')
62 system_err.text = test.fail_tb
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020063 return testcase
64
65def trial_to_text(trial):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020066 suite_failures = []
67 count_fail = 0
68 count_pass = 0
69 for suite in trial.suites:
70 if suite.passed():
71 count_pass += 1
72 else:
73 count_fail += 1
74 suite_failures.append(suite_to_text(suite))
75
76 summary = ['%s: %s' % (trial.name(), trial.status)]
77 if count_fail:
78 summary.append('%d suites failed' % count_fail)
79 if count_pass:
80 summary.append('%d suites passed' % count_pass)
81 msg = [', '.join(summary)]
82 msg.extend(suite_failures)
83 return '\n'.join(msg)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020084
85def suite_to_text(suite):
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +020086 if not suite.tests:
87 return 'no tests were run.'
Neels Hofmeyr6ccda112017-06-06 19:41:17 +020088
89 passed, skipped, failed = suite.count_test_results()
90 details = []
91 if failed:
92 details.append('fail: %d' % failed)
93 if passed:
94 details.append('pass: %d' % passed)
95 if skipped:
96 details.append('skip: %d' % skipped)
97 msgs = ['%s: %s (%s)' % (suite.status, suite.name(), ', '.join(details))]
98 msgs.extend([test_to_text(t) for t in suite.tests])
99 return '\n '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200100
101def test_to_text(test):
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200102 msgs = ['%s: %s' % (test.status, test.name())]
103 if test.start_timestamp:
104 msgs.append('(%.1f sec)' % test.duration)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200105 if test.status == suite.Test.FAIL:
Neels Hofmeyr6ccda112017-06-06 19:41:17 +0200106 msgs.append('%s: %s' % (test.fail_type, test.fail_message))
107 return ' '.join(msgs)
Pau Espin Pedrol0ffb4142017-05-15 18:24:35 +0200108
109# vim: expandtab tabstop=4 shiftwidth=4