From 356ee65cfb57b300a9666974351965ba20fe9e94 Mon Sep 17 00:00:00 2001 From: oatssss Date: Tue, 16 Mar 2021 21:20:41 -0400 Subject: [PATCH 1/7] Add Behave support for Dynamic.link --- allure-behave/src/listener.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/allure-behave/src/listener.py b/allure-behave/src/listener.py index beecae67..0bdccf9b 100644 --- a/allure-behave/src/listener.py +++ b/allure-behave/src/listener.py @@ -6,12 +6,12 @@ from allure_commons.utils import uuid4 from allure_commons.utils import now from allure_commons.utils import platform_label -from allure_commons.types import LabelType, AttachmentType +from allure_commons.types import LabelType, AttachmentType, LinkType from allure_commons.model2 import TestResult from allure_commons.model2 import TestStepResult from allure_commons.model2 import TestBeforeResult, TestAfterResult from allure_commons.model2 import TestResultContainer -from allure_commons.model2 import Parameter, Label +from allure_commons.model2 import Parameter, Label, Link from allure_behave.utils import scenario_parameters from allure_behave.utils import scenario_name from allure_behave.utils import scenario_history_id @@ -33,6 +33,8 @@ class AllureListener(object): def __init__(self, behave_config): self.behave_config = behave_config + self.issue_pattern = behave_config.userdata.get('AllureFormatter.issue_pattern', None) + self.link_pattern = behave_config.userdata.get('AllureFormatter.link_pattern', None) self.logger = AllureReporter() self.current_step_uuid = None self.current_scenario_uuid = None @@ -100,9 +102,7 @@ def start_scenario(self, scenario): test_case.description = '\n'.join(scenario.description) test_case.parameters = scenario_parameters(scenario) - issue_pattern = self.behave_config.userdata.get('AllureFormatter.issue_pattern', None) - link_pattern = self.behave_config.userdata.get('AllureFormatter.link_pattern', None) - test_case.links.extend(scenario_links(scenario, issue_pattern=issue_pattern, link_pattern=link_pattern)) + test_case.links.extend(scenario_links(scenario, issue_pattern=self.issue_pattern, link_pattern=self.link_pattern)) test_case.labels.extend(scenario_labels(scenario)) test_case.labels.append(Label(name=LabelType.FEATURE, value=scenario.feature.name)) test_case.labels.append(Label(name=LabelType.FRAMEWORK, value='behave')) @@ -191,6 +191,23 @@ def attach_data(self, body, name, attachment_type, extension): def attach_file(self, source, name, attachment_type, extension): self.logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension) + @allure_commons.hookimpl + def add_link(self, url, link_type, name): + test_result = self.logger.get_test(None) + if test_result: + pattern = u'{}' + if link_type == LinkType.ISSUE and self.issue_pattern: + pattern = self.issue_pattern + elif link_type == LinkType.LINK and self.link_pattern: + pattern = self.link_pattern + + link_url = pattern.format(url) + new_link = Link(link_type, link_url, link_url if name is None else name) + for link in test_result.links: + if link.url == new_link.url: + return + + test_result.links.append(new_link) class Context(list): def __init__(self, _list=list()): From 480970cf404419210ea22525c1fba7c430aeaa03 Mon Sep 17 00:00:00 2001 From: oatssss Date: Wed, 17 Mar 2021 01:51:34 -0400 Subject: [PATCH 2/7] Add test case for behave dynamic links --- allure-behave/features/link.feature | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/allure-behave/features/link.feature b/allure-behave/features/link.feature index fdc82ccd..82e490ae 100644 --- a/allure-behave/features/link.feature +++ b/allure-behave/features/link.feature @@ -1,6 +1,6 @@ Feature: Link - Scenario: Scenario issue link + Scenario: Scenario issue link Given feature definition """ Feature: Step status @@ -26,7 +26,6 @@ Feature: Link Then allure report has a scenario with name "Scenario with passed step" And scenario has "http://qameta.io" link - Scenario: Feature and scenario user link Given feature definition """ @@ -41,3 +40,26 @@ Feature: Link Then allure report has a scenario with name "Scenario with passed step" And scenario has "http://qameta.io" link And scenario has "http://example.com" link with type "issue" + + Scenario: Dynamic link + Given feature definition + """ + Feature: Step status + + Scenario: Scenario with passed step + Given simple passed step + """ + And hooks implementation + """ + import allure + import allure_commons + + @allure_commons.fixture + def before_scenario(context, scenario): + allure.dynamic.link("http://qameta.io") + allure.dynamic.issue("http://example.com") + """ + When I run behave with allure formatter + Then allure report has a scenario with name "Scenario with passed step" + And scenario has "http://qameta.io" link + And scenario has "http://example.com" link with type "issue" \ No newline at end of file From 4c053cc78be777419fd1a08f913d4dcb1b997e9e Mon Sep 17 00:00:00 2001 From: oatssss Date: Wed, 17 Mar 2021 02:16:11 -0400 Subject: [PATCH 3/7] Double line is required --- allure-behave/src/listener.py | 1 + 1 file changed, 1 insertion(+) diff --git a/allure-behave/src/listener.py b/allure-behave/src/listener.py index 0bdccf9b..6aa6f647 100644 --- a/allure-behave/src/listener.py +++ b/allure-behave/src/listener.py @@ -209,6 +209,7 @@ def add_link(self, url, link_type, name): test_result.links.append(new_link) + class Context(list): def __init__(self, _list=list()): super(Context, self).__init__(_list) From f7e92c74a3f5cadeec0e70d5d126f2379923f170 Mon Sep 17 00:00:00 2001 From: oatssss Date: Wed, 17 Mar 2021 02:18:33 -0400 Subject: [PATCH 4/7] Meet line length requirements --- allure-behave/src/listener.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/allure-behave/src/listener.py b/allure-behave/src/listener.py index 6aa6f647..549ddd9f 100644 --- a/allure-behave/src/listener.py +++ b/allure-behave/src/listener.py @@ -102,7 +102,9 @@ def start_scenario(self, scenario): test_case.description = '\n'.join(scenario.description) test_case.parameters = scenario_parameters(scenario) - test_case.links.extend(scenario_links(scenario, issue_pattern=self.issue_pattern, link_pattern=self.link_pattern)) + test_case.links.extend(scenario_links(scenario, + issue_pattern=self.issue_pattern, + link_pattern=self.link_pattern)) test_case.labels.extend(scenario_labels(scenario)) test_case.labels.append(Label(name=LabelType.FEATURE, value=scenario.feature.name)) test_case.labels.append(Label(name=LabelType.FRAMEWORK, value='behave')) From e2d7d226af4bc9f40a2174a4353c4caa7edede9e Mon Sep 17 00:00:00 2001 From: oatssss Date: Wed, 17 Mar 2021 02:21:04 -0400 Subject: [PATCH 5/7] Fix PEP8 E128 --- allure-behave/src/listener.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/allure-behave/src/listener.py b/allure-behave/src/listener.py index 549ddd9f..8517b8ea 100644 --- a/allure-behave/src/listener.py +++ b/allure-behave/src/listener.py @@ -102,7 +102,8 @@ def start_scenario(self, scenario): test_case.description = '\n'.join(scenario.description) test_case.parameters = scenario_parameters(scenario) - test_case.links.extend(scenario_links(scenario, + test_case.links.extend(scenario_links( + scenario, issue_pattern=self.issue_pattern, link_pattern=self.link_pattern)) test_case.labels.extend(scenario_labels(scenario)) From a408c628cd9417596ce1e3c3c806a0e2fd786a53 Mon Sep 17 00:00:00 2001 From: oatssss Date: Thu, 18 Mar 2021 14:00:42 -0400 Subject: [PATCH 6/7] Implement dynamic descriptions in behave listener --- allure-behave/src/listener.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/allure-behave/src/listener.py b/allure-behave/src/listener.py index 8517b8ea..b3931505 100644 --- a/allure-behave/src/listener.py +++ b/allure-behave/src/listener.py @@ -194,6 +194,18 @@ def attach_data(self, body, name, attachment_type, extension): def attach_file(self, source, name, attachment_type, extension): self.logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension) + @allure_commons.hookimpl + def add_description(self, test_description): + test_result = self.logger.get_test(None) + if test_result: + test_result.description = test_description + + @allure_commons.hookimpl + def add_description_html(self, test_description_html): + test_result = self.logger.get_test(None) + if test_result: + test_result.descriptionHtml = test_description_html + @allure_commons.hookimpl def add_link(self, url, link_type, name): test_result = self.logger.get_test(None) From 2982a3e05b664db49cbc5a60cac7c13afd182659 Mon Sep 17 00:00:00 2001 From: oatssss Date: Thu, 18 Mar 2021 14:04:47 -0400 Subject: [PATCH 7/7] Add behave test case for dynamic description --- allure-behave/features/description.feature | 21 ++++++++++++++++++++ allure-behave/features/steps/report_steps.py | 10 +++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/allure-behave/features/description.feature b/allure-behave/features/description.feature index 5e8b343c..3a8633b6 100644 --- a/allure-behave/features/description.feature +++ b/allure-behave/features/description.feature @@ -13,3 +13,24 @@ Feature: Description """ When I run behave with allure formatter Then allure report has a scenario with name "Scenario with description" + + Scenario: Dynamic description + Given feature definition + """ + Feature: Step status + + Scenario: Scenario with passed step + Given simple passed step + """ + And hooks implementation + """ + import allure + import allure_commons + + @allure_commons.fixture + def before_scenario(context, scenario): + allure.dynamic.description("Test description") + """ + When I run behave with allure formatter + Then allure report has a scenario with name "Scenario with passed step" + And scenario has description "Test description" \ No newline at end of file diff --git a/allure-behave/features/steps/report_steps.py b/allure-behave/features/steps/report_steps.py index d332314e..24f7fa51 100644 --- a/allure-behave/features/steps/report_steps.py +++ b/allure-behave/features/steps/report_steps.py @@ -1,5 +1,5 @@ from functools import partial -from hamcrest import assert_that +from hamcrest import assert_that, contains_string from hamcrest import not_ from allure_commons_test.report import has_test_case from allure_commons_test.result import with_status @@ -9,6 +9,7 @@ from allure_commons_test.result import has_status_details from allure_commons_test.result import with_message_contains from allure_commons_test.result import has_link +from allure_commons_test.result import has_description from allure_commons_test.container import has_container from allure_commons_test.container import has_before, has_after from allure_commons_test.label import has_severity @@ -149,3 +150,10 @@ def step_attachment(context, item): context_matcher = getattr(context, item) matcher = partial(context_matcher, has_attachment) assert_that(context.allure_report, matcher()) + + +@then(u'scenario has description "{description}"') +def step_description(context, description): + context_matcher = context.scenario + matcher = partial(context_matcher, has_description, contains_string(description)) + assert_that(context.allure_report, matcher())