blob: 90b4d6b7ed4acccad125c82485b1aab9e4472759 [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
46 return
47
48 if not result.wasSuccessful():
49 failures = (result.errors + result.failures +
50 result.unexpectedSuccesses)
51 if failures:
52 test, bt = failures[0]
53 name = test.test_data.get('name', test.id())
54 msg = 'From test "%s" :\n%s' % (name, bt)
55 test_class_instance.fail(msg)
56
57 test_class_instance.assertTrue(result.wasSuccessful())
58
59
60def test_maker(test_dir, filename, name):
61 def test(self):
62 self._prep_test(filename)
63 run_test(self, test_dir, filename)
64 test.__name__ = name
65 return test
66
67
68def generate_tests(test_class, test_dir):
69 # Create one scenario per yaml file
70 filenames = os.listdir(test_dir)
71 if not filenames:
72 raise RuntimeError("%s is empty" % test_dir)
73 for filename in filenames:
74 if not filename.endswith('.yaml'):
75 continue
76 name = "test_%s" % filename[:-5].lower().replace("-", "_")
77 setattr(test_class, name,
78 test_maker(test_dir, filename, name))