Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 1 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 8 | # it under the terms of the GNU General Public License as |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 9 | # 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 Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 15 | # GNU General Public License for more details. |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 16 | # |
Harald Welte | 2720534 | 2017-06-03 09:51:45 +0200 | [diff] [blame] | 17 | # You should have received a copy of the GNU General Public License |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 20 | import math |
| 21 | from datetime import datetime |
| 22 | import xml.etree.ElementTree as et |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 23 | from . import test |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 24 | |
| 25 | def trial_to_junit_write(trial, junit_path): |
| 26 | elements = et.ElementTree(element=trial_to_junit(trial)) |
| 27 | elements.write(junit_path) |
| 28 | |
| 29 | def 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 | |
| 36 | def suite_to_junit(suite): |
| 37 | testsuite = et.Element('testsuite') |
| 38 | testsuite.set('name', suite.name()) |
| 39 | testsuite.set('hostname', 'localhost') |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 40 | 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 43 | testsuite.set('tests', str(len(suite.tests))) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 44 | testsuite.set('failures', str(suite.count_test_results()[2])) |
Holger Hans Peter Freyther | f922a99 | 2019-02-27 04:35:17 +0000 | [diff] [blame] | 45 | for suite_test in suite.tests: |
| 46 | testcase = test_to_junit(suite_test) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 47 | testsuite.append(testcase) |
| 48 | return testsuite |
| 49 | |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 50 | def test_to_junit(t): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 51 | testcase = et.Element('testcase') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 52 | testcase.set('name', t.name()) |
| 53 | testcase.set('time', str(math.ceil(t.duration))) |
| 54 | if t.status == test.Test.SKIP: |
Holger Hans Peter Freyther | d03acdf | 2018-09-23 18:06:48 +0100 | [diff] [blame] | 55 | et.SubElement(testcase, 'skipped') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 56 | elif t.status == test.Test.FAIL: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 57 | failure = et.SubElement(testcase, 'failure') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 58 | failure.set('type', t.fail_type or 'failure') |
| 59 | failure.text = t.fail_message |
| 60 | if t.fail_tb: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 61 | system_err = et.SubElement(testcase, 'system-err') |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 62 | system_err.text = t.fail_tb |
| 63 | elif t.status != test.Test.PASS: |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 64 | error = et.SubElement(testcase, 'error') |
| 65 | error.text = 'could not run' |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 66 | return testcase |
| 67 | |
| 68 | def trial_to_text(trial): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 69 | 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 87 | |
| 88 | def suite_to_text(suite): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 89 | if not suite.tests: |
| 90 | return 'no tests were run.' |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 91 | |
| 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 103 | |
Pau Espin Pedrol | fd5de3d | 2017-11-09 14:26:35 +0100 | [diff] [blame] | 104 | def 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 Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 110 | return ' '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 111 | |
| 112 | # vim: expandtab tabstop=4 shiftwidth=4 |