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 | |
| 20 | import os |
| 21 | import math |
| 22 | from datetime import datetime |
| 23 | import xml.etree.ElementTree as et |
| 24 | from . import log, suite |
| 25 | |
| 26 | def trial_to_junit_write(trial, junit_path): |
| 27 | elements = et.ElementTree(element=trial_to_junit(trial)) |
| 28 | elements.write(junit_path) |
| 29 | |
| 30 | def 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 | |
| 37 | def suite_to_junit(suite): |
| 38 | testsuite = et.Element('testsuite') |
| 39 | testsuite.set('name', suite.name()) |
| 40 | testsuite.set('hostname', 'localhost') |
Neels Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 41 | 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 44 | testsuite.set('tests', str(len(suite.tests))) |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 45 | testsuite.set('failures', str(suite.count_test_results()[2])) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 46 | for test in suite.tests: |
| 47 | testcase = test_to_junit(test) |
| 48 | testsuite.append(testcase) |
| 49 | return testsuite |
| 50 | |
| 51 | def 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 Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 58 | 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 Hofmeyr | f8e6186 | 2017-06-06 23:08:07 +0200 | [diff] [blame] | 64 | elif test.status != suite.Test.PASS: |
| 65 | error = et.SubElement(testcase, 'error') |
| 66 | error.text = 'could not run' |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 67 | return testcase |
| 68 | |
| 69 | def trial_to_text(trial): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 70 | 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 88 | |
| 89 | def suite_to_text(suite): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 90 | if not suite.tests: |
| 91 | return 'no tests were run.' |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 92 | |
| 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 104 | |
| 105 | def test_to_text(test): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 106 | msgs = ['%s: %s' % (test.status, test.name())] |
| 107 | if test.start_timestamp: |
| 108 | msgs.append('(%.1f sec)' % test.duration) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 109 | if test.status == suite.Test.FAIL: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame] | 110 | msgs.append('%s: %s' % (test.fail_type, test.fail_message)) |
| 111 | return ' '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 112 | |
| 113 | # vim: expandtab tabstop=4 shiftwidth=4 |