def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory, host, port, fixture_module, intercept): """Generate a TestSuite from YAML data.""" file_suite = gabbi_suite.GabbiSuite() test_data = test_yaml['tests'] fixtures = test_yaml.get('fixtures', None) # Set defaults from BASE_TESTS then update those defaults # with any defaults set in the YAML file. base_test_data = copy.deepcopy(case.HTTPTestCase.base_test) defaults = test_yaml.get('defaults', {}) test_update(base_test_data, defaults) # Establish any fixture classes. fixture_classes = [] if fixtures and fixture_module: for fixture_class in fixtures: fixture_classes.append(getattr(fixture_module, fixture_class)) prior_test = None base_test_key_set = set(case.HTTPTestCase.base_test.keys()) for test_datum in test_data: test = copy.deepcopy(base_test_data) test_update(test, test_datum) if not test['name']: raise AssertionError('Test name missing in a test in %s.' % test_base_name) test_name = '%s_%s' % (test_base_name, test['name'].lower().replace( ' ', '_')) test_key_set = set(test.keys()) if test_key_set != base_test_key_set: raise AssertionError('Invalid test keys used in test %s: %s' % (test_name, test_key_set - base_test_key_set)) # Use metaclasses to build a class of the necessary type # and name with relevant arguments. http_class = utils.get_http(verbose=test['verbose']) klass = TestBuilder( test_name, (case.HTTPTestCase, ), { 'test_data': test, 'test_directory': test_directory, 'fixtures': fixture_classes, 'http': http_class, 'host': host, 'intercept': intercept, 'port': port, 'prior': prior_test }) tests = loader.loadTestsFromTestCase(klass) this_test = tests._tests[0] file_suite.addTest(this_test) prior_test = this_test return file_suite
def test_suite_from_dict(loader, test_base_name, suite_dict, test_directory, host, port, fixture_module, intercept, prefix=''): """Generate a TestSuite from YAML-sourced data.""" try: test_data = suite_dict['tests'] except KeyError: raise GabbiFormatError('malformed test file, "tests" key required') except TypeError: # `suite_dict` appears not to be a dictionary; we cannot infer # any details or suggestions on how to fix it, thus discarding # the original exception in favor of a generic error raise GabbiFormatError('malformed test file, invalid format') # Merge global with per-suite defaults default_test_dict = copy.deepcopy(case.HTTPTestCase.base_test) local_defaults = _validate_defaults(suite_dict.get('defaults', {})) test_update(default_test_dict, local_defaults) # Establish any fixture classes used in this file. fixtures = suite_dict.get('fixtures', None) fixture_classes = [] if fixtures and fixture_module: for fixture_class in fixtures: fixture_classes.append(getattr(fixture_module, fixture_class)) test_maker = TestMaker(test_base_name, default_test_dict, test_directory, fixture_classes, loader, host, port, intercept, prefix) file_suite = gabbi_suite.GabbiSuite() prior_test = None for test_dict in test_data: this_test = test_maker.make_one_test(test_dict, prior_test) file_suite.addTest(this_test) prior_test = this_test return file_suite
def test_suite_from_dict(loader, test_base_name, suite_dict, test_directory, host, port, fixture_module, intercept, prefix='', handlers=None, test_loader_name=None, inner_fixtures=None): """Generate a GabbiSuite from a dict represent a list of tests. The dict takes the form: :param fixtures: An optional list of fixture classes that this suite can use. :param defaults: An optional dictionary of default values to be used in each test. :param tests: A list of individual tests, themselves each being a dictionary. See :data:`gabbi.case.BASE_TEST`. """ try: test_data = suite_dict['tests'] except KeyError: raise GabbiFormatError('malformed test file, "tests" key required') except TypeError: # `suite_dict` appears not to be a dictionary; we cannot infer # any details or suggestions on how to fix it, thus discarding # the original exception in favor of a generic error raise GabbiFormatError('malformed test file, invalid format') handlers = handlers or [] response_handlers = [] content_handlers = [] # Merge global with per-suite defaults default_test_dict = copy.deepcopy(case.HTTPTestCase.base_test) seen_keys = set() for handler in handlers: default_test_dict.update(copy.deepcopy(handler.test_base)) if handler.response_handler: if handler.test_key_suffix not in seen_keys: response_handlers.append(handler.response_handler) seen_keys.add(handler.test_key_suffix) if handler.content_handler: content_handlers.append(handler.content_handler) local_defaults = _validate_defaults(suite_dict.get('defaults', {})) test_update(default_test_dict, local_defaults) # Establish any fixture classes used in this file. fixtures = suite_dict.get('fixtures', None) fixture_classes = [] if fixtures and fixture_module: for fixture_class in fixtures: fixture_classes.append(getattr(fixture_module, fixture_class)) test_maker = TestMaker(test_base_name, default_test_dict, test_directory, fixture_classes, loader, host, port, intercept, prefix, response_handlers, content_handlers, test_loader_name=test_loader_name, inner_fixtures=inner_fixtures) file_suite = suite.GabbiSuite() prior_test = None for test_dict in test_data: this_test = test_maker.make_one_test(test_dict, prior_test) file_suite.addTest(this_test) prior_test = this_test return file_suite