예제 #1
0
def _validate_testcase(func):
    refsig = method(func.__name__, ['env', 'result'])
    actualsig = MethodSignature(func.__name__, getargspec(func), lambda x: x)

    if refsig != actualsig:
        raise MethodSignatureMismatch('Expected {0}, not {1}'.format(
            refsig, actualsig))
예제 #2
0
        def _wrapper():
            case_result = self.cfg.result(
                stdout_style=self.stdout_style,
                _scratch=self.scratch,
            )

            testcase_report = TestCaseReport(
                name='{} - {}'.format(label, func.__name__),
                description=func.__doc__,
            )

            num_args = len(callable_utils.getargspec(func).args)
            args = (self.resources,) if num_args == 1 else (
                self.resources, case_result)

            with testcase_report.timer.record('run'):
                with testcase_report.logged_exceptions():
                    func(*args)

            testcase_report.extend(case_result.serialized_entries)

            if self.get_stdout_style(testcase_report.passed).display_case:
                self.log_testcase_status(testcase_report)

            self.pre_post_step_report.append(testcase_report)
예제 #3
0
파일: base.py 프로젝트: chrlag/testplan
        def _wrapper():
            case_result = self.cfg.result(stdout_style=self.stdout_style,
                                          _scratch=self.scratch)

            testcase_report = testplan.report.TestCaseReport(
                name="{} - {}".format(label, func.__name__),
                description=strings.get_docstring(func),
            )

            num_args = len(callable_utils.getargspec(func).args)
            args = ((self.resources, ) if num_args == 1 else
                    (self.resources, case_result))

            with testcase_report.timer.record("run"):
                with testcase_report.logged_exceptions():
                    func(*args)

            testcase_report.extend(case_result.serialized_entries)
            testcase_report.attachments.extend(case_result.attachments)

            if self.get_stdout_style(testcase_report.passed).display_testcase:
                self.log_testcase_status(testcase_report)

            testcase_report.pass_if_empty()
            self.pre_post_step_report.append(testcase_report)
예제 #4
0
def _validate_skip_if_predicates(predicates):
    """
    Check for method signature, set / extend ``skip_funcs`` attribute of
    the testcase method.
    """
    for predicate in predicates:
        refsig = static_method(predicate.__name__, ['suite'])
        actualsig = MethodSignature(
            predicate.__name__, getargspec(predicate), lambda x: x)

        if refsig != actualsig:
            raise MethodSignatureMismatch('Expected {0}, not {1}'.format(
                refsig, actualsig))

    return predicates
예제 #5
0
def _check_tag_func(tag_func):
    """Make sure ``tag_func`` is a callable that takes ``kwargs`` arguments"""
    if tag_func is None:
        return

    if not callable(tag_func):
        raise ParametrizationError("tag_func must be a callable.")

    argspec = callable_utils.getargspec(tag_func)

    if len(argspec.args) == 1:
        arg_1 = argspec.args[0]
        if arg_1 == "kwargs":
            return

    raise ParametrizationError(
        'tag_func must be a callable that takes 1 argument named "kwargs".'
        " (e.g. def custom_tag_func(kwargs): ...")
예제 #6
0
def _check_name_func(name_func):
    """
    Make sure ``name_func`` is a callable that takes
    ``func_name``, ``kwargs`` arguments.
    """
    if not callable(name_func):
        raise ParametrizationError("name_func must be a callable.")

    argspec = callable_utils.getargspec(name_func)

    if len(argspec.args) == 2:
        arg_1, arg_2 = argspec.args
        if arg_1 == "func_name" and arg_2 == "kwargs":
            return

    raise ParametrizationError(
        "name_func must be a callable that takes 2 arguments "
        'named "func_name" and "kwargs".'
        " (e.g. def custom_name_func(func_name, kwargs): ...")
예제 #7
0
def generate_functions(
    function,
    parameters,
    name,
    name_func,
    tag_dict,
    tag_func,
    docstring_func,
    summarize,
    num_passing,
    num_failing,
    key_combs_limit,
    execution_group,
    timeout,
):
    """
    Generate test cases using the given parameter context, use the name_func
    to generate the name.

    If parameters is of type ``tuple`` / ``list`` then a new testcase method
    will be created for each item.

    If parameters is of type ``dict`` (of ``tuple``/``list``), then a new
    method will be created for each item in the Cartesian product of all
    combinations of values.

    :param function: A testcase method, with extra arguments
                     for parametrization.
    :type function: ``callable``
    :param parameters: Parametrization context for the test case method.
    :type parameters: ``list`` or ``tuple`` of ``dict`` or ``tuple`` / ``list``
                      OR a ``dict`` of ``tuple`` / ``list``.
    :param name: Customized readable name for testcase.
    :type name: ``str``
    :param name_func: Function for generating names of parametrized testcases,
                      should accept ``func_name`` and ``kwargs`` as parameters.
    :type name_func: ``callable``
    :param docstring_func: Function that will generate docstring,
                      should accept ``docstring`` and ``kwargs`` as parameters.
    :type docstring_func: ``callable``
    :param tag_func: Function that will be used for generating tags via
                     parametrization kwargs. Should accept ``kwargs`` as
                     parameter.
    :type tag_func: ``callable``
    :param tag_dict: Tag annotations to be used for each generated testcase.
    :type tag_dict: ``dict`` of ``set``
    :param summarize: Flag for enabling testcase level
                      summarization of all assertions.
    :type summarize: ``bool``
    :param num_passing: Max number of passing assertions
                       for testcase level assertion summary.
    :type num_passing: ``int``
    :param num_failing: Max number of failing assertions
                       for testcase level assertion summary.
    :type num_failing: ``int``
    :param key_combs_limit: Max number of failed key combinations on fix/dict
                            summaries that contain assertion details.
    :type key_combs_limit: ``int``
    :param execution_group: Name of execution group in which the testcases
                            can be executed in parallel.
    :type execution_group: ``str``
    :param timeout: Timeout in seconds to wait for testcase to be finished.
    :type timeout: ``int``
    :return: List of functions that is testcase compliant
             (accepts ``self``, ``env``, ``result`` as arguments) and have
             unique names.
    :rtype: ``list``
    """
    if not parameters:
        raise ParametrizationError('"parameters" cannot be a empty.')

    _check_name_func(name_func)

    argspec = callable_utils.getargspec(function)
    args = argspec.args[3:]  # get rid of self, env, result
    defaults = argspec.defaults or []

    required_args = args[:-len(defaults)] if defaults else args
    default_args = dict(zip(args[len(required_args):], defaults))

    kwarg_list = _generate_kwarg_list(parameters, args, required_args,
                                      default_args)

    functions = [
        _generate_func(
            function=function,
            name=name,
            name_func=name_func,
            tag_func=tag_func,
            docstring_func=docstring_func,
            tag_dict=tag_dict,
            kwargs=kwargs,
        ) for kwargs in kwarg_list
    ]

    for func in functions:
        func.summarize = summarize
        func.summarize_num_passing = num_passing
        func.summarize_num_failing = num_failing
        func.summarize_key_combs_limit = key_combs_limit
        func.execution_group = execution_group
        func.timeout = timeout

    return functions