def test_step_parameters(executed_docstring_source, request, args, kwargs): """ >>> import pytest >>> import allure >>> @allure.step ... def step(arg, kwarg=None): ... pass >>> @pytest.mark.parametrize( ... ["args", "kwargs"], ... [ ... ([True], {"kwarg": False}), ... ([True], {"kwarg": False}), ... (["hi"], {"kwarg": None}), ... ([None], {"kwarg": 42}) ... ] ... ) ... def test_args_less_than_placeholders_example(args, kwargs): ... step(*args, **kwargs) """ test_name = "test_args_less_than_placeholders_example[{params_name}]".format( params_name=params_name(request)) assert_that( executed_docstring_source.allure_report, has_test_case( test_name, has_step( "step", *([has_parameter("arg", represent(arg)) for arg in args] + [ has_parameter("kwarg", represent(kwarg)) for kwarg in kwargs.values() ]))))
def test_step_parameters(executed_docstring_source, request, args, kwargs): """ >>> import pytest >>> import allure >>> @allure.step ... def step(arg, kwarg=None): ... pass >>> @pytest.mark.parametrize( ... ["args", "kwargs"], ... [ ... ([True], {"kwarg": False}), ... ([True], {"kwarg": False}), ... (["hi"], {"kwarg": None}), ... ([None], {"kwarg": 42}) ... ] ... ) ... def test_args_less_than_placeholders_example(args, kwargs): ... step(*args, **kwargs) """ test_name = "test_args_less_than_placeholders_example[{params_name}]".format( params_name=params_name(request)) assert_that(executed_docstring_source.allure_report, has_test_case(test_name, has_step("step", *([has_parameter("arg", represent(arg)) for arg in args] + [has_parameter("kwarg", represent(kwarg)) for kwarg in kwargs.values()] ) ) ) )
def mark_to_str(marker): args = [represent(arg) for arg in marker.args] kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] if args or kwargs: parameters = ', '.join(args + kwargs) return '@pytest.mark.{name}({parameters})'.format(name=marker.name, parameters=parameters) else: return '@pytest.mark.{name}'.format(name=marker.name)
def mark_to_str(marker): args = [represent(arg) for arg in marker.args] kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] markstr = '{name}'.format(name=marker.name) if args or kwargs: parameters = ', '.join(args + kwargs) markstr = '{}({})'.format(markstr, parameters) return markstr
def mark_to_str(marker): args = [represent(arg) for arg in marker.args] kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] if marker.name in ('filterwarnings', 'skip', 'skipif', 'xfail', 'tryfirst', 'trylast'): markstr = '@pytest.mark.{name}'.format(name=marker.name) else: markstr = '{name}'.format(name=marker.name) if args or kwargs: parameters = ', '.join(args + kwargs) markstr = '{}({})'.format(markstr, parameters) return markstr
def pytest_runtest_setup(self, item): if not self._cache.get(item.nodeid): uuid = self._cache.push(item.nodeid) test_result = TestResult(name=item.name, uuid=uuid, start=now(), stop=now()) self.allure_logger.schedule_test(uuid, test_result) yield uuid = self._cache.get(item.nodeid) test_result = self.allure_logger.get_test(uuid) for fixturedef in _test_fixtures(item): group_uuid = self._cache.get(fixturedef) if not group_uuid: group_uuid = self._cache.push(fixturedef) group = TestResultContainer(uuid=group_uuid) self.allure_logger.start_group(group_uuid, group) self.allure_logger.update_group(group_uuid, children=uuid) params = item.callspec.params if hasattr(item, 'callspec') else {} test_result.name = allure_name(item, params) test_result.description = allure_description(item) test_result.descriptionHtml = allure_description_html(item) test_result.fullName = allure_full_name(item) test_result.historyId = md5(test_result.fullName) test_result.parameters.extend( [Parameter(name=name, value=represent(value)) for name, value in params.items()])
def pytest_runtest_setup(self, item): uuid = self._cache.set(item.nodeid) test_result = TestResult(name=item.name, uuid=uuid) self.allure_logger.schedule_test(uuid, test_result) yield uuid = self._cache.get(item.nodeid) test_result = self.allure_logger.get_test(uuid) for fixturedef in _test_fixtures(item): group_uuid = self._cache.get(fixturedef) if not group_uuid: group_uuid = self._cache.set(fixturedef) group = TestResultContainer(uuid=group_uuid) self.allure_logger.start_group(group_uuid, group) self.allure_logger.update_group(group_uuid, children=uuid) params = item.callspec.params if hasattr(item, 'callspec') else {} test_result.name = allure_name(item, params) test_result.description = allure_description(item) test_result.descriptionHtml = allure_description_html(item) test_result.fullName = allure_full_name(item) test_result.historyId = md5(test_result.fullName) test_result.parameters.extend([ Parameter(name=name, value=represent(value)) for name, value in params.items() ])
def pytest_runtest_call(self, item): uuid = self._cache.get(item.nodeid) for name, value in item.callspec.params.items() if hasattr(item, 'callspec') else (): self.allure_logger.update_test(uuid, parameters=Parameter(name, represent(value))) self.allure_logger.update_test(uuid, start=now()) yield self.allure_logger.update_test(uuid, stop=now())
def test_parametrized_func(first, second): """ >>> from nose2.tools import params >>> @params(("hello", 42), ("world", 777)) ... def test_parametrized_func_example(alpha, betta): ... pass """ first_param_name, first_param_value = first second_param_name, second_param_value = second allure_report = run_docstring_example() assert_that( allure_report, has_test_case( "test_parametrized_func_example", has_parameter(first_param_name, represent(first_param_value)), has_parameter(second_param_name, represent(second_param_value))))
def test_parametrized_method(self, first, second): """ >>> import unittest >>> from nose2.tools import params >>> class TestParametrizedExample(unittest.TestCase): ... @params(({"hello": 4}, [4, 2]), ({"wold": 2}, [7, 7, 7])) ... def test_parametrized_method_example(self, bravo, charlie): ... pass """ first_param_name, first_param_value = first second_param_name, second_param_value = second allure_report = run_docstring_example() assert_that( allure_report, has_test_case( "test_parametrized_method_example", has_parameter(first_param_name, represent(first_param_value)), has_parameter(second_param_name, represent(second_param_value))))
def pytest_runtest_protocol(self, item, nextitem): uuid = self._cache.set(item.nodeid) for fixturedef in _test_fixtures(item): group_uuid = self._cache.get(fixturedef) if not group_uuid: group_uuid = self._cache.set(fixturedef) group = TestResultContainer(uuid=group_uuid) self.allure_logger.start_group(group_uuid, group) self.allure_logger.update_group(group_uuid, children=uuid) test_case = TestResult(name=allure_name(item), uuid=uuid) self.allure_logger.schedule_test(uuid, test_case) if hasattr(item, 'function'): test_case.description = item.function.__doc__ yield for name, value in item.callspec.params.items() if hasattr( item, 'callspec') else (): test_result = self.allure_logger.get_test(uuid) if test_result: test_result.parameters.append(Parameter( name, represent(value))) test_case.labels.extend([ Label(name=name, value=value) for name, value in allure_labels(item) ]) test_case.labels.extend([ Label(name=LabelType.TAG, value=value) for value in pytest_markers(item) ]) test_case.labels.append(Label(name=LabelType.HOST, value=self._host)) test_case.labels.append( Label(name=LabelType.THREAD, value=self._thread)) test_case.labels.append(Label(name=LabelType.FRAMEWORK, value='pytest')) test_case.labels.append( Label(name=LabelType.LANGUAGE, value=platform_label())) test_case.links += [ Link(link_type, url, name) for link_type, url, name in allure_links(item) ] test_case.fullName = allure_full_name(item) test_case.historyId = md5(test_case.fullName) test_case.labels.append(Label('package', allure_package(item))) uuid = self._cache.pop(item.nodeid) self.allure_logger.close_test(uuid)
def test_pytest_marker_with_kwargs_utf_encoding(executed_docstring_source): """ >>> import pytest >>> @pytest.mark.marker(stuff=u'я') ... def test_pytest_marker_with_kwargs_utf_encoding_example(): ... pass """ assert_that( executed_docstring_source.allure_report, has_test_case("test_pytest_marker_with_kwargs_utf_encoding_example", has_tag("marker(stuff=%s)" % represent('я'))))
def test_pytest_marker_with_kwargs_utf_encoding(executed_docstring_source): """ >>> import pytest >>> @pytest.mark.marker(stuff=u'я') ... def test_pytest_marker_with_kwargs_utf_encoding_example(): ... pass """ assert_that(executed_docstring_source.allure_report, has_test_case("test_pytest_marker_with_kwargs_utf_encoding_example", has_tag("marker(stuff=%s)" % represent('я')) ) )
def _fn_params_to_ordered_dict(func, *args, **kwargs): spec = inspect.getfullargspec(func) # given pos_or_named = list of pos_only args and pos_or_named/standard args pos_or_named_ordered_names = list(spec.args) pos_without_defaults_dict = dict(zip(spec.args, args)) if spec.args and spec.args[0] in ['cls', 'self']: pos_without_defaults_dict.pop(spec.args[0], None) received_args_amount = len(args) pos_or_named_not_set = spec.args[received_args_amount:] pos_defaults_dict = \ dict(zip(pos_or_named_not_set, spec.defaults or [])) varargs = args[len(spec.args):] varargs_dict = \ {spec.varargs: varargs} if (spec.varargs and varargs) else \ {} pos_or_named_or_vargs_ordered_names = \ pos_or_named_ordered_names + [spec.varargs] if varargs_dict else \ pos_or_named_ordered_names pos_or_named_or_vargs_or_named_only_ordered_names = ( pos_or_named_or_vargs_ordered_names + list(spec.kwonlyargs) ) items = { **pos_without_defaults_dict, **pos_defaults_dict, **varargs_dict, **(spec.kwonlydefaults or {}), **kwargs, }.items() sorted_items = sorted( map(lambda kv: (kv[0], represent(kv[1])), items), key= lambda x: pos_or_named_or_vargs_or_named_only_ordered_names.index(x[0]) ) return collections.OrderedDict(sorted_items)
def _params(names, values): return [ Parameter(name=name, value=represent(value)) for name, value in zip(names, values) ]
def impl(*a, **kw): __tracebackhide__ = True params = func_parameters(func, *a, **kw) args = list(map(lambda x: represent(x), a)) with StepContext(self.title.format(*args, **params), params): return func(*a, **kw)
def impl(*a, **kw): __tracebackhide__ = True params = func_parameters(func, *a, **kw) args = list(map(lambda x: represent(x), a)) with StepContext(self.title.format(*args, **params), params): return func(*a, **kw)