예제 #1
0
class RunnableTestCase(BaseTestCase):

    def __init__(self, tc_data, parent, defaults):
        BaseTestCase.__init__(self, tc_data.name, parent)
        self.doc = tc_data.doc.value
        self.setup = defaults.get_setup(tc_data.setup)
        self.teardown = defaults.get_teardown(tc_data.teardown)
        self.tags = defaults.get_tags(tc_data.tags)
        self.timeout = defaults.get_timeout(tc_data.timeout)
        template = defaults.get_template(tc_data.template)
        self.keywords = Keywords(tc_data.steps, template)

    def run(self, context, suite_errors):
        self._suite_errors = suite_errors
        self._start_run(context)
        if self.run_errors.is_allowed_to_run():
            self._run(context)
        else:
            self._not_allowed_to_run()
        self._end_run(context)

    def _start_run(self, context):
        self.run_errors = TestRunErrors(self._suite_errors)
        self.status = 'RUNNING'
        self.starttime = utils.get_timestamp()
        self.run_errors.init_err(self._init_test(context))
        context.start_test(self)

    def _init_test(self, context):
        errors = []
        self.doc = context.replace_vars_from_setting('Documentation', self.doc,
                                                     errors)
        self.setup.replace_variables(context.get_current_vars(), errors)
        self.teardown.replace_variables(context.get_current_vars(), errors)
        self.tags = utils.normalize_tags(context.replace_vars_from_setting('Tags',
                                                                           self.tags,
                                                                           errors))
        self.timeout.replace_variables(context.get_current_vars())
        if errors:
            return 'Test case initialization failed:\n%s' % '\n'.join(errors)
        if not self.name:
            return 'Test case name is required.'
        if not self.keywords:
            return 'Test case contains no keywords'
        return None

    def _run(self, context):
        self.timeout.start()
        self._run_setup(context)
        if not self.run_errors.setup_failed():
            try:
                self.keywords.run(context)
            except ExecutionFailed, err:
                self.run_errors.kw_err(unicode(err))
                self.keyword_failed(err)
        context.set_test_status_before_teardown(*self._report_status())
        self._run_teardown(context)
        self._report_status_after_teardown()
예제 #2
0
class RunnableTestCase(BaseTestCase):
    def __init__(self, tc_data, parent, defaults):
        BaseTestCase.__init__(self, tc_data.name, parent)
        self.doc = tc_data.doc.value
        self.setup = defaults.get_setup(tc_data.setup)
        self.teardown = defaults.get_teardown(tc_data.teardown)
        self.tags = defaults.get_tags(tc_data.tags)
        self.timeout = defaults.get_timeout(tc_data.timeout)
        template = defaults.get_template(tc_data.template)
        self.keywords = Keywords(tc_data.steps, template)

    def run(self, context, suite_errors):
        self._suite_errors = suite_errors
        self._start_run(context)
        if self.run_errors.is_allowed_to_run():
            self._run(context)
        else:
            self._not_allowed_to_run()
        self._end_run(context)

    def _start_run(self, context):
        self.run_errors = TestRunErrors(self._suite_errors)
        self.status = 'RUNNING'
        self.starttime = utils.get_timestamp()
        self.run_errors.init_err(self._init_test(context))
        context.start_test(self)

    def _init_test(self, context):
        errors = []
        self.doc = context.replace_vars_from_setting('Documentation', self.doc,
                                                     errors)
        self.setup.replace_variables(context.get_current_vars(), errors)
        self.teardown.replace_variables(context.get_current_vars(), errors)
        self.tags = utils.normalize_tags(
            context.replace_vars_from_setting('Tags', self.tags, errors))
        self.timeout.replace_variables(context.get_current_vars())
        if errors:
            return 'Test case initialization failed:\n%s' % '\n'.join(errors)
        if not self.name:
            return 'Test case name is required.'
        if not self.keywords:
            return 'Test case contains no keywords'
        return None

    def _run(self, context):
        self.timeout.start()
        self._run_setup(context)
        if not self.run_errors.setup_failed():
            try:
                self.keywords.run(context)
            except ExecutionFailed, err:
                self.run_errors.kw_err(unicode(err))
                self.keyword_failed(err)
        context.set_test_status_before_teardown(*self._report_status())
        self._run_teardown(context)
        self._report_status_after_teardown()
예제 #3
0
class UserKeywordHandler(object):
    type = "user"

    def __init__(self, keyword, libname):
        self.name = keyword.name
        self.keywords = Keywords(keyword.steps)
        self.return_value = keyword.return_.value
        self._libname = libname
        self.doc = self._doc = keyword.doc.value
        self._timeout = keyword.timeout
        self._keyword_args = keyword.args.value

    @property
    def longname(self):
        return "%s.%s" % (self._libname, self.name) if self._libname else self.name

    @property
    def shortdoc(self):
        return self.doc.splitlines()[0] if self.doc else ""

    def init_keyword(self, varz):
        self._errors = []
        self.doc = varz.replace_meta("Documentation", self._doc, self._errors)
        self.timeout = KeywordTimeout(self._timeout.value, self._timeout.message)
        self.timeout.replace_variables(varz)

    def run(self, context, arguments):
        context.namespace.start_user_keyword(self)
        try:
            return self._run(context, arguments)
        finally:
            context.namespace.end_user_keyword()

    def _run(self, context, argument_values):
        args_spec = UserKeywordArguments(self._keyword_args, self.longname)
        variables = context.get_current_vars()
        if context.dry_run:
            return self._dry_run(context, variables, args_spec, argument_values)
        return self._variable_resolving_run(context, variables, args_spec, argument_values)

    def _dry_run(self, context, variables, args_spec, argument_values):
        resolved_arguments = args_spec.resolve_arguments_for_dry_run(argument_values)
        self._execute(context, variables, args_spec, resolved_arguments)
        return None

    def _variable_resolving_run(self, context, variables, args_spec, argument_values):
        resolved_arguments = args_spec.resolve(argument_values, variables)
        self._execute(context, variables, args_spec, resolved_arguments)
        return self._get_return_value(variables)

    def _execute(self, context, variables, args_spec, resolved_arguments):
        args_spec.set_variables(resolved_arguments, variables, context.output)
        self._verify_keyword_is_valid()
        self.timeout.start()
        self.keywords.run(context)

    def _verify_keyword_is_valid(self):
        if self._errors:
            raise DataError("User keyword initialization failed:\n%s" % "\n".join(self._errors))
        if not (self.keywords or self.return_value):
            raise DataError("User keyword '%s' contains no keywords" % self.name)

    def _get_return_value(self, variables):
        if not self.return_value:
            return None
        try:
            ret = variables.replace_list(self.return_value)
        except DataError, err:
            raise DataError("Replacing variables from keyword return value " "failed: %s" % unicode(err))
        if len(ret) != 1 or is_list_var(self.return_value[0]):
            return ret
        return ret[0]
예제 #4
0
class UserKeywordHandler(object):
    type = 'user'

    def __init__(self, keyword, libname):
        self.name = keyword.name
        self.keywords = Keywords(keyword.steps)
        self.return_value = keyword.return_.value
        self._libname = libname
        self.doc = self._doc = keyword.doc.value
        self._timeout = keyword.timeout
        self._keyword_args = keyword.args.value

    @property
    def longname(self):
        return '%s.%s' % (self._libname,
                          self.name) if self._libname else self.name

    @property
    def shortdoc(self):
        return self.doc.splitlines()[0] if self.doc else ''

    def init_keyword(self, varz):
        self._errors = []
        self.doc = varz.replace_meta('Documentation', self._doc, self._errors)
        self.timeout = KeywordTimeout(self._timeout.value,
                                      self._timeout.message)
        self.timeout.replace_variables(varz)

    def run(self, context, arguments):
        context.namespace.start_user_keyword(self)
        try:
            return self._run(context, arguments)
        finally:
            context.namespace.end_user_keyword()

    def _run(self, context, argument_values):
        args_spec = UserKeywordArguments(self._keyword_args, self.longname)
        variables = context.get_current_vars()
        if context.dry_run:
            return self._dry_run(context, variables, args_spec,
                                 argument_values)
        return self._variable_resolving_run(context, variables, args_spec,
                                            argument_values)

    def _dry_run(self, context, variables, args_spec, argument_values):
        resolved_arguments = args_spec.resolve_arguments_for_dry_run(
            argument_values)
        self._execute(context, variables, args_spec, resolved_arguments)
        return None

    def _variable_resolving_run(self, context, variables, args_spec,
                                argument_values):
        resolved_arguments = args_spec.resolve(argument_values, variables)
        self._execute(context, variables, args_spec, resolved_arguments)
        return self._get_return_value(variables)

    def _execute(self, context, variables, args_spec, resolved_arguments):
        args_spec.set_variables(resolved_arguments, variables, context.output)
        self._verify_keyword_is_valid()
        self.timeout.start()
        self.keywords.run(context)

    def _verify_keyword_is_valid(self):
        if self._errors:
            raise DataError('User keyword initialization failed:\n%s' %
                            '\n'.join(self._errors))
        if not (self.keywords or self.return_value):
            raise DataError("User keyword '%s' contains no keywords" %
                            self.name)

    def _get_return_value(self, variables):
        if not self.return_value:
            return None
        try:
            ret = variables.replace_list(self.return_value)
        except DataError, err:
            raise DataError('Replacing variables from keyword return value '
                            'failed: %s' % unicode(err))
        if len(ret) != 1 or is_list_var(self.return_value[0]):
            return ret
        return ret[0]
class UserKeywordHandler(object):
    type = 'user'

    def __init__(self, keyword, libname):
        self.name = keyword.name
        self.keywords = Keywords(keyword.steps)
        self.return_value = keyword.return_.value
        self.teardown = keyword.teardown
        self.libname = libname
        self.doc = self._doc = keyword.doc.value
        self._timeout = keyword.timeout
        self._keyword_args = keyword.args.value

    @property
    def longname(self):
        return '%s.%s' % (self.libname, self.name) if self.libname else self.name

    @property
    def shortdoc(self):
        return self.doc.splitlines()[0] if self.doc else ''

    def init_keyword(self, varz):
        self._errors = []
        self.doc = varz.replace_meta('Documentation', self._doc, self._errors)
        self.timeout = KeywordTimeout(self._timeout.value, self._timeout.message)
        self.timeout.replace_variables(varz)

    def run(self, context, arguments):
        context.namespace.start_user_keyword(self)
        try:
            return self._run(context, arguments)
        finally:
            context.namespace.end_user_keyword()

    def _run(self, context, argument_values):
        args_spec = UserKeywordArguments(self._keyword_args, self.longname)
        variables = context.get_current_vars()
        if context.dry_run:
            return self._dry_run(context, variables, args_spec, argument_values)
        return self._variable_resolving_run(context, variables, args_spec, argument_values)

    def _dry_run(self, context, variables, args_spec, argument_values):
        resolved_arguments = args_spec.resolve_arguments_for_dry_run(argument_values)
        self._execute(context, variables, args_spec, resolved_arguments)
        return None

    def _variable_resolving_run(self, context, variables, args_spec, argument_values):
        resolved_arguments = args_spec.resolve(argument_values, variables,
                                               context.output)
        self._execute(context, variables, args_spec, resolved_arguments)
        return self._get_return_value(variables)

    def _execute(self, context, variables, args_spec, resolved_arguments):
        args_spec.set_variables(resolved_arguments, variables, context.output)
        self._verify_keyword_is_valid()
        self.timeout.start()
        try:
            self.keywords.run(context)
        except ExecutionFailed, error:
            pass
        else: