Beispiel #1
0
    def make_one_test(self, test_dict, prior_test):
        """Create one single HTTPTestCase.

        The returned HTTPTestCase is added to the TestSuite currently
        being built (one per YAML file).
        """
        test = copy.deepcopy(self.test_defaults)
        try:
            test_update(test, test_dict)
        except KeyError as exc:
            raise GabbiFormatError('invalid key in test: %s' % exc)
        except AttributeError as exc:
            if not isinstance(test_dict, dict):
                raise GabbiFormatError('test chunk is not a dict at "%s"' %
                                       test_dict)
            else:
                # NOTE(cdent): Not clear this can happen but just in case.
                raise GabbiFormatError('malformed test chunk "%s": %s' %
                                       (test_dict, exc))

        test_name = self._set_test_name(test)
        self._set_test_method_and_url(test, test_name)
        self._validate_keys(test, test_name)

        http_class = httpclient.get_http(verbose=test['verbose'],
                                         caption=test['name'])
        if prior_test:
            history = prior_test.history
        else:
            history = {}

        # Use metaclasses to build a class of the necessary type
        # and name with relevant arguments.
        klass = TestBuilder(
            test_name, (case.HTTPTestCase, ), {
                'test_data': test,
                'test_directory': self.test_directory,
                'fixtures': self.fixture_classes,
                'inner_fixtures': self.inner_fixtures,
                'http': http_class,
                'host': self.host,
                'intercept': self.intercept,
                'content_handlers': self.content_handlers,
                'response_handlers': self.response_handlers,
                'port': self.port,
                'prefix': self.prefix,
                'prior': prior_test,
                'history': history,
            })
        # We've been asked to, make this test class think it comes
        # from a different module.
        if self.test_loader_name:
            klass.__module__ = self.test_loader_name
        check_host = case.testcase.skipIf(not self.host, 'No host configured')

        tests = self.loader.loadTestsFromTestCase(check_host(klass))
        history[test['name']] = tests._tests[0]
        # Return the first (and only) test in the klass.
        return tests._tests[0]
Beispiel #2
0
    def make_one_test(self, test_dict, prior_test):
        """Create one single HTTPTestCase.

        The returned HTTPTestCase is added to the TestSuite currently
        being built (one per YAML file).
        """
        test = copy.deepcopy(self.test_defaults)
        try:
            test_update(test, test_dict)
        except KeyError as exc:
            raise GabbiFormatError('invalid key in test: %s' % exc)
        except AttributeError as exc:
            if not isinstance(test_dict, dict):
                raise GabbiFormatError('test chunk is not a dict at "%s"' %
                                       test_dict)
            else:
                # NOTE(cdent): Not clear this can happen but just in case.
                raise GabbiFormatError('malformed test chunk "%s": %s' %
                                       (test_dict, exc))

        test_name = self._set_test_name(test)
        self._set_test_method_and_url(test, test_name)
        self._validate_keys(test, test_name)

        http_class = httpclient.get_http(verbose=test['verbose'],
                                         caption=test['name'])

        # Use metaclasses to build a class of the necessary type
        # and name with relevant arguments.
        klass = TestBuilder(
            test_name, (case.HTTPTestCase, ), {
                'test_data': test,
                'test_directory': self.test_directory,
                'fixtures': self.fixture_classes,
                'http': http_class,
                'host': self.host,
                'intercept': self.intercept,
                'port': self.port,
                'prefix': self.prefix,
                'prior': prior_test
            })

        tests = self.loader.loadTestsFromTestCase(klass)
        # Return the first (and only) test in the klass.
        return tests._tests[0]
Beispiel #3
0
    def make_one_test(self, test_dict, prior_test):
        """Create one single HTTPTestCase.

        The returned HTTPTestCase is added to the TestSuite currently
        being built (one per YAML file).
        """
        test = copy.deepcopy(self.test_defaults)
        try:
            test_update(test, test_dict)
        except KeyError as exc:
            raise GabbiFormatError('invalid key in test: %s' % exc)
        except AttributeError as exc:
            if not isinstance(test_dict, dict):
                raise GabbiFormatError(
                    'test chunk is not a dict at "%s"' % test_dict)
            else:
                # NOTE(cdent): Not clear this can happen but just in case.
                raise GabbiFormatError(
                    'malformed test chunk "%s": %s' % (test_dict, exc))

        test_name = self._set_test_name(test)
        self._set_test_method_and_url(test, test_name)
        self._validate_keys(test, test_name)

        http_class = httpclient.get_http(verbose=test['verbose'],
                                         caption=test['name'])

        # Use metaclasses to build a class of the necessary type
        # and name with relevant arguments.
        klass = TestBuilder(test_name, (case.HTTPTestCase,),
                            {'test_data': test,
                             'test_directory': self.test_directory,
                             'fixtures': self.fixture_classes,
                             'http': http_class,
                             'host': self.host,
                             'intercept': self.intercept,
                             'port': self.port,
                             'prefix': self.prefix,
                             'prior': prior_test})

        tests = self.loader.loadTestsFromTestCase(klass)
        # Return the first (and only) test in the klass.
        return tests._tests[0]
Beispiel #4
0
def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory,
                         host, port, fixture_module, intercept, prefix=None):
    """Generate a TestSuite from YAML data."""

    file_suite = gabbi_suite.GabbiSuite()
    try:
        test_data = test_yaml['tests']
    except KeyError:
        raise GabbiFormatError(
            'malformed test file, "tests" key required')
    except TypeError:
        # Swallow this exception as displaying it does not shine a
        # light on the path to fix it.
        raise GabbiFormatError('malformed test file, invalid format')

    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 = _validate_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)
        try:
            test_update(test, test_datum)
        except AttributeError as exc:
            if not isinstance(test_datum, dict):
                raise GabbiFormatError(
                    'test chunk is not a dict at "%s"' % test_datum)
            else:
                # NOTE(cdent): Not clear this can ever happen but just in
                # case.
                raise GabbiFormatError(
                    'malformed test chunk "%s": %s' % (test_datum, exc))

        if not test['name']:
            raise GabbiFormatError('Test name missing in a test in %s.'
                                   % test_base_name)
        test_name = '%s_%s' % (test_base_name,
                               test['name'].lower().replace(' ', '_'))

        # use uppercase keys as HTTP method
        method_key = None
        for key, val in six.iteritems(test):
            if _is_method_shortcut(key):
                if method_key:
                    raise GabbiFormatError(
                        'duplicate method/URL directive in "%s"' %
                        test_name)

                test['method'] = key
                test['url'] = val
                method_key = key
        if method_key:
            del test[method_key]

        if not test['url']:
            raise GabbiFormatError('Test url missing in test %s.'
                                   % test_name)

        test_key_set = set(test.keys())
        if test_key_set != base_test_key_set:
            raise GabbiFormatError(
                'Invalid test keys used in test %s: %s'
                % (test_name,
                   ', '.join(list(test_key_set - base_test_key_set))))

        # Use metaclasses to build a class of the necessary type
        # and name with relevant arguments.
        http_class = httpclient.get_http(verbose=test['verbose'],
                                         caption=test_name)
        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,
                             'prefix': prefix,
                             'prior': prior_test})

        tests = loader.loadTestsFromTestCase(klass)
        this_test = tests._tests[0]
        file_suite.addTest(this_test)
        prior_test = this_test

    return file_suite
Beispiel #5
0
    def make_one_test(self, test_dict, prior_test):
        """Create one single HTTPTestCase.

        The returned HTTPTestCase is added to the TestSuite currently
        being built (one per YAML file).
        """
        test = copy.deepcopy(self.test_defaults)
        try:
            test_update(test, test_dict)
        except KeyError as exc:
            raise GabbiFormatError('invalid key in test: %s' % exc)
        except AttributeError as exc:
            if not isinstance(test_dict, dict):
                raise GabbiFormatError(
                    'test chunk is not a dict at "%s"' % test_dict)
            else:
                # NOTE(cdent): Not clear this can happen but just in case.
                raise GabbiFormatError(
                    'malformed test chunk "%s": %s' % (test_dict, exc))

        test_name = self._set_test_name(test)
        self._set_test_method_and_url(test, test_name)
        self._validate_keys(test, test_name)

        http_class = httpclient.get_http(verbose=test['verbose'],
                                         caption=test['name'])
        if prior_test:
            history = prior_test.history
        else:
            history = {}

        test_method_name = 'test_request'
        test_method = getattr(case.HTTPTestCase, test_method_name)

        @case.testcase.skipIf(self.host == '', 'No host configured')
        @functools.wraps(test_method)
        def do_test(*args, **kwargs):
            return test_method(*args, **kwargs)

        # Use metaclasses to build a class of the necessary type
        # and name with relevant arguments.
        klass = TestBuilder(test_name, (case.HTTPTestCase,),
                            {'test_data': test,
                             'test_directory': self.test_directory,
                             'fixtures': self.fixture_classes,
                             'inner_fixtures': self.inner_fixtures,
                             'http': http_class,
                             'host': self.host,
                             'intercept': self.intercept,
                             'content_handlers': self.content_handlers,
                             'response_handlers': self.response_handlers,
                             'port': self.port,
                             'prefix': self.prefix,
                             'prior': prior_test,
                             'history': history,
                             test_method_name: do_test,
                             })
        # We've been asked to, make this test class think it comes
        # from a different module.
        if self.test_loader_name:
            klass.__module__ = self.test_loader_name

        tests = self.loader.loadTestsFromTestCase(klass)
        history[test['name']] = tests._tests[0]
        # Return the first (and only) test in the klass.
        return tests._tests[0]