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') |
| 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 Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame^] | 44 | testsuite.set('failures', str(suite.count_test_results()[2])) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 45 | for test in suite.tests: |
| 46 | testcase = test_to_junit(test) |
| 47 | testsuite.append(testcase) |
| 48 | return testsuite |
| 49 | |
| 50 | def 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 Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame^] | 57 | 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 63 | return testcase |
| 64 | |
| 65 | def trial_to_text(trial): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame^] | 66 | 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 84 | |
| 85 | def suite_to_text(suite): |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 86 | if not suite.tests: |
| 87 | return 'no tests were run.' |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame^] | 88 | |
| 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 Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 100 | |
| 101 | def test_to_text(test): |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame^] | 102 | msgs = ['%s: %s' % (test.status, test.name())] |
| 103 | if test.start_timestamp: |
| 104 | msgs.append('(%.1f sec)' % test.duration) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 105 | if test.status == suite.Test.FAIL: |
Neels Hofmeyr | 6ccda11 | 2017-06-06 19:41:17 +0200 | [diff] [blame^] | 106 | msgs.append('%s: %s' % (test.fail_type, test.fail_message)) |
| 107 | return ' '.join(msgs) |
Pau Espin Pedrol | 0ffb414 | 2017-05-15 18:24:35 +0200 | [diff] [blame] | 108 | |
| 109 | # vim: expandtab tabstop=4 shiftwidth=4 |