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 |
| 8 | # it under the terms of the GNU Affero General Public License as |
| 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 |
| 15 | # GNU Affero General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Affero General Public License |
| 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))) |
| 44 | testsuite.set('failures', str(suite.test_failed_ctr)) |
| 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: |
| 57 | failure = et.SubElement(testcase, 'failure') |
| 58 | failure.set('type', test.fail_type) |
| 59 | failure.text = test.fail_message |
| 60 | return testcase |
| 61 | |
| 62 | def trial_to_text(trial): |
| 63 | msg = '\n%s [%s]\n ' % (trial.status, trial.name()) |
| 64 | msg += '\n '.join(suite_to_text(result) for result in trial.suites) |
| 65 | return msg |
| 66 | |
| 67 | def suite_to_text(suite): |
| 68 | if suite.test_failed_ctr: |
| 69 | return 'FAIL: [%s] %d failed out of %d tests run (%d skipped):\n %s' % ( |
| 70 | suite.name(), suite.test_failed_ctr, len(suite.tests), suite.test_skipped_ctr, |
| 71 | '\n '.join([test_to_text(t) for t in suite.tests])) |
| 72 | if not suite.tests: |
| 73 | return 'no tests were run.' |
| 74 | return 'pass: all %d tests passed (%d skipped).' % (len(suite.tests), suite.test_skipped_ctr) |
| 75 | |
| 76 | def test_to_text(test): |
| 77 | ret = "%s: [%s]" % (test.status, test.name()) |
| 78 | if test.status != suite.Test.SKIP: |
| 79 | ret += " (%s, %d sec)" % (datetime.fromtimestamp(round(test.start_timestamp)).isoformat(), test.duration) |
| 80 | if test.status == suite.Test.FAIL: |
| 81 | ret += " type:'%s' message: %s" % (test.fail_type, test.fail_message.replace('\n', '\n ')) |
| 82 | return ret |
| 83 | |
| 84 | # vim: expandtab tabstop=4 shiftwidth=4 |