blob: 2b51160bf5b402150816f4fc6e1e5b14d0569120 [file] [log] [blame]
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +02001# 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
13from __future__ import absolute_import
14
15import os
16import unittest
17
18from gabbi import runner
19from gabbi import suitemaker
20from gabbi import utils
21from oslo_log import log as logging
22
23LOG = logging.getLogger(__name__)
24
25
26def run_test(test_class_instance, test_dir, filename):
27 d = utils.load_yaml(yaml_file=os.path.join(test_dir, filename))
28 test_suite = suitemaker.test_suite_from_dict(
29 loader=unittest.defaultTestLoader,
30 test_base_name="gabbi",
31 suite_dict=d,
32 test_directory=test_dir,
33 host='example.com', port=None,
34 fixture_module=None,
35 intercept=None,
36 handlers=runner.initialize_handlers([]),
37 test_loader_name="tempest")
38
39 # NOTE(sileht): We hide stdout/stderr and reraise the failure
40 # manually, tempest will print it ittest_class.
41 with open(os.devnull, 'w') as stream:
42 result = unittest.TextTestRunner(
43 stream=stream, verbosity=0, failfast=True,
44 ).run(test_suite)
45
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020046 if not result.wasSuccessful():
Andreas Jaeger7716ce02020-05-03 11:11:49 +020047 failures = (result.errors + result.failures
48 + result.unexpectedSuccesses)
Mehdi Abaakoukb64d2b02018-09-03 11:50:49 +020049 if failures:
50 test, bt = failures[0]
51 name = test.test_data.get('name', test.id())
52 msg = 'From test "%s" :\n%s' % (name, bt)
53 test_class_instance.fail(msg)
54
55 test_class_instance.assertTrue(result.wasSuccessful())
56
57
58def test_maker(test_dir, filename, name):
59 def test(self):
60 self._prep_test(filename)
61 run_test(self, test_dir, filename)
62 test.__name__ = name
63 return test
64
65
66def generate_tests(test_class, test_dir):
67 # Create one scenario per yaml file
68 filenames = os.listdir(test_dir)
69 if not filenames:
70 raise RuntimeError("%s is empty" % test_dir)
71 for filename in filenames:
72 if not filename.endswith('.yaml'):
73 continue
74 name = "test_%s" % filename[:-5].lower().replace("-", "_")
75 setattr(test_class, name,
76 test_maker(test_dir, filename, name))