Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 13 | import os |
| 14 | import unittest |
| 15 | |
| 16 | from gabbi import runner |
| 17 | from gabbi import suitemaker |
| 18 | from gabbi import utils |
| 19 | from oslo_log import log as logging |
| 20 | |
| 21 | LOG = logging.getLogger(__name__) |
| 22 | |
| 23 | |
| 24 | def run_test(test_class_instance, test_dir, filename): |
| 25 | d = utils.load_yaml(yaml_file=os.path.join(test_dir, filename)) |
| 26 | test_suite = suitemaker.test_suite_from_dict( |
| 27 | loader=unittest.defaultTestLoader, |
| 28 | test_base_name="gabbi", |
| 29 | suite_dict=d, |
| 30 | test_directory=test_dir, |
| 31 | host='example.com', port=None, |
| 32 | fixture_module=None, |
| 33 | intercept=None, |
| 34 | handlers=runner.initialize_handlers([]), |
| 35 | test_loader_name="tempest") |
| 36 | |
| 37 | # NOTE(sileht): We hide stdout/stderr and reraise the failure |
| 38 | # manually, tempest will print it ittest_class. |
| 39 | with open(os.devnull, 'w') as stream: |
| 40 | result = unittest.TextTestRunner( |
| 41 | stream=stream, verbosity=0, failfast=True, |
| 42 | ).run(test_suite) |
| 43 | |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 44 | if not result.wasSuccessful(): |
Andreas Jaeger | 7716ce0 | 2020-05-03 11:11:49 +0200 | [diff] [blame] | 45 | failures = (result.errors + result.failures |
| 46 | + result.unexpectedSuccesses) |
Mehdi Abaakouk | b64d2b0 | 2018-09-03 11:50:49 +0200 | [diff] [blame] | 47 | if failures: |
| 48 | test, bt = failures[0] |
| 49 | name = test.test_data.get('name', test.id()) |
| 50 | msg = 'From test "%s" :\n%s' % (name, bt) |
| 51 | test_class_instance.fail(msg) |
| 52 | |
| 53 | test_class_instance.assertTrue(result.wasSuccessful()) |
| 54 | |
| 55 | |
| 56 | def test_maker(test_dir, filename, name): |
| 57 | def test(self): |
| 58 | self._prep_test(filename) |
| 59 | run_test(self, test_dir, filename) |
| 60 | test.__name__ = name |
| 61 | return test |
| 62 | |
| 63 | |
| 64 | def generate_tests(test_class, test_dir): |
| 65 | # Create one scenario per yaml file |
| 66 | filenames = os.listdir(test_dir) |
| 67 | if not filenames: |
| 68 | raise RuntimeError("%s is empty" % test_dir) |
| 69 | for filename in filenames: |
| 70 | if not filename.endswith('.yaml'): |
| 71 | continue |
| 72 | name = "test_%s" % filename[:-5].lower().replace("-", "_") |
| 73 | setattr(test_class, name, |
| 74 | test_maker(test_dir, filename, name)) |