Stefan Kobza | b189933 | 2015-12-23 17:00:10 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at: |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
| 16 | import os |
| 17 | import robot |
| 18 | from robot.run import RobotFramework |
| 19 | from robot.conf.settings import RobotSettings |
| 20 | from robot.running.builder import TestSuiteBuilder |
| 21 | from robot.running.model import TestSuite |
| 22 | |
| 23 | |
| 24 | def get_suite_list(*datasources, **options): |
| 25 | class _MyRobotFramework(RobotFramework): |
| 26 | def main(self, datasources, **options): |
| 27 | # copied from robot.run.RobotFramework.main |
| 28 | settings = RobotSettings(options) |
| 29 | suite = TestSuiteBuilder(settings['SuiteNames'], |
| 30 | settings['WarnOnSkipped']).build(*datasources) |
| 31 | suite.configure(**settings.suite_config) |
| 32 | |
| 33 | return suite |
| 34 | |
| 35 | # Options are in robot.conf.settings |
| 36 | suite = _MyRobotFramework().execute(*datasources, **options) |
| 37 | if isinstance(suite, TestSuite): |
| 38 | suites = [] |
| 39 | suites.append(suite) |
| 40 | append_new = True |
| 41 | while append_new: |
| 42 | append_new = False |
| 43 | tmp = [] |
| 44 | for s in suites: |
| 45 | if len(s.suites._items) > 0: |
| 46 | for i in s.suites._items: |
| 47 | tmp.append(i) |
| 48 | append_new = True |
| 49 | else: |
| 50 | tmp.append(s) |
| 51 | suites = tmp |
| 52 | return suites |
| 53 | else: |
| 54 | # TODO: add from robot.errors typ error |
| 55 | return [] |
| 56 | |
| 57 | |
| 58 | def run_suites(test_dir, suites, out_dir="./outputs", **options): |
| 59 | # TODO: add logic |
| 60 | |
| 61 | try: |
| 62 | for f in os.listdir(out_dir): |
| 63 | os.remove('/'.join((out_dir, f))) |
| 64 | except OSError: |
| 65 | pass |
| 66 | if not os.path.exists(out_dir): |
| 67 | os.makedirs(out_dir) |
| 68 | |
| 69 | for s in suites: |
| 70 | longname = s.longname |
| 71 | varfile=[] |
| 72 | varfile.append('resources/libraries/python/topology.py') |
| 73 | |
| 74 | # TODO: check testcases Tags |
| 75 | |
| 76 | with open('{}/{}.out'.format(out_dir, longname), 'w') as out, \ |
| 77 | open('{}/{}.log'.format(out_dir, longname), 'w') as debug: |
| 78 | robot.run(test_dir, |
| 79 | suite=[longname], |
| 80 | output='{}/{}.xml'.format(out_dir, longname), |
| 81 | debugfile=debug, |
| 82 | log=None, |
| 83 | report=None, |
| 84 | stdout=out, |
| 85 | variablefile=varfile, |
| 86 | **options) |
| 87 | |
| 88 | |
| 89 | def parse_outputs(out_dir='./'): |
| 90 | outs = ['/'.join((out_dir, file)) for file in os.listdir(out_dir) if file.endswith('.xml')] |
| 91 | robot.rebot(*outs, merge=True) |
| 92 | |
| 93 | |
| 94 | if __name__ == "__main__": |
| 95 | i = [] |
| 96 | e = [] |
| 97 | # i = ['bd', 'ip'] |
| 98 | # i = ['hw'] |
| 99 | # e = ['hw'] |
| 100 | test_dir = "./tests" |
| 101 | out_dir = "./outputs" |
| 102 | |
| 103 | suite_list = get_suite_list(test_dir, include=i, exclude=e, output=None, dryrun=True) |
| 104 | run_suites(test_dir, suite_list, include=i, exclude=e, out_dir=out_dir) |
| 105 | parse_outputs(out_dir=out_dir) |