Skip to content

Commit 61da8af

Browse files
authored
support Dynamic.link and Dynamic.description for allure-behave (via #574)
1 parent fa4e2ff commit 61da8af

File tree

4 files changed

+92
-8
lines changed

4 files changed

+92
-8
lines changed

allure-behave/features/description.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,24 @@ Feature: Description
1313
"""
1414
When I run behave with allure formatter
1515
Then allure report has a scenario with name "Scenario with description"
16+
17+
Scenario: Dynamic description
18+
Given feature definition
19+
"""
20+
Feature: Step status
21+
22+
Scenario: Scenario with passed step
23+
Given simple passed step
24+
"""
25+
And hooks implementation
26+
"""
27+
import allure
28+
import allure_commons
29+
30+
@allure_commons.fixture
31+
def before_scenario(context, scenario):
32+
allure.dynamic.description("Test description")
33+
"""
34+
When I run behave with allure formatter
35+
Then allure report has a scenario with name "Scenario with passed step"
36+
And scenario has description "Test description"

allure-behave/features/link.feature

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Feature: Link
22

3-
Scenario: Scenario issue link
3+
Scenario: Scenario issue link
44
Given feature definition
55
"""
66
Feature: Step status
@@ -26,7 +26,6 @@ Feature: Link
2626
Then allure report has a scenario with name "Scenario with passed step"
2727
And scenario has "http://qameta.io" link
2828

29-
3029
Scenario: Feature and scenario user link
3130
Given feature definition
3231
"""
@@ -41,3 +40,26 @@ Feature: Link
4140
Then allure report has a scenario with name "Scenario with passed step"
4241
And scenario has "http://qameta.io" link
4342
And scenario has "http://example.com" link with type "issue"
43+
44+
Scenario: Dynamic link
45+
Given feature definition
46+
"""
47+
Feature: Step status
48+
49+
Scenario: Scenario with passed step
50+
Given simple passed step
51+
"""
52+
And hooks implementation
53+
"""
54+
import allure
55+
import allure_commons
56+
57+
@allure_commons.fixture
58+
def before_scenario(context, scenario):
59+
allure.dynamic.link("http://qameta.io")
60+
allure.dynamic.issue("http://example.com")
61+
"""
62+
When I run behave with allure formatter
63+
Then allure report has a scenario with name "Scenario with passed step"
64+
And scenario has "http://qameta.io" link
65+
And scenario has "http://example.com" link with type "issue"

allure-behave/features/steps/report_steps.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import partial
2-
from hamcrest import assert_that
2+
from hamcrest import assert_that, contains_string
33
from hamcrest import not_
44
from allure_commons_test.report import has_test_case
55
from allure_commons_test.result import with_status
@@ -9,6 +9,7 @@
99
from allure_commons_test.result import has_status_details
1010
from allure_commons_test.result import with_message_contains
1111
from allure_commons_test.result import has_link
12+
from allure_commons_test.result import has_description
1213
from allure_commons_test.container import has_container
1314
from allure_commons_test.container import has_before, has_after
1415
from allure_commons_test.label import has_severity
@@ -149,3 +150,10 @@ def step_attachment(context, item):
149150
context_matcher = getattr(context, item)
150151
matcher = partial(context_matcher, has_attachment)
151152
assert_that(context.allure_report, matcher())
153+
154+
155+
@then(u'scenario has description "{description}"')
156+
def step_description(context, description):
157+
context_matcher = context.scenario
158+
matcher = partial(context_matcher, has_description, contains_string(description))
159+
assert_that(context.allure_report, matcher())

allure-behave/src/listener.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
from allure_commons.utils import uuid4
77
from allure_commons.utils import now
88
from allure_commons.utils import platform_label
9-
from allure_commons.types import LabelType, AttachmentType
9+
from allure_commons.types import LabelType, AttachmentType, LinkType
1010
from allure_commons.model2 import TestResult
1111
from allure_commons.model2 import TestStepResult
1212
from allure_commons.model2 import TestBeforeResult, TestAfterResult
1313
from allure_commons.model2 import TestResultContainer
14-
from allure_commons.model2 import Parameter, Label
14+
from allure_commons.model2 import Parameter, Label, Link
1515
from allure_behave.utils import scenario_parameters
1616
from allure_behave.utils import scenario_name
1717
from allure_behave.utils import scenario_history_id
@@ -33,6 +33,8 @@
3333
class AllureListener(object):
3434
def __init__(self, behave_config):
3535
self.behave_config = behave_config
36+
self.issue_pattern = behave_config.userdata.get('AllureFormatter.issue_pattern', None)
37+
self.link_pattern = behave_config.userdata.get('AllureFormatter.link_pattern', None)
3638
self.logger = AllureReporter()
3739
self.current_step_uuid = None
3840
self.current_scenario_uuid = None
@@ -100,9 +102,10 @@ def start_scenario(self, scenario):
100102
test_case.description = '\n'.join(scenario.description)
101103
test_case.parameters = scenario_parameters(scenario)
102104

103-
issue_pattern = self.behave_config.userdata.get('AllureFormatter.issue_pattern', None)
104-
link_pattern = self.behave_config.userdata.get('AllureFormatter.link_pattern', None)
105-
test_case.links.extend(scenario_links(scenario, issue_pattern=issue_pattern, link_pattern=link_pattern))
105+
test_case.links.extend(scenario_links(
106+
scenario,
107+
issue_pattern=self.issue_pattern,
108+
link_pattern=self.link_pattern))
106109
test_case.labels.extend(scenario_labels(scenario))
107110
test_case.labels.append(Label(name=LabelType.FEATURE, value=scenario.feature.name))
108111
test_case.labels.append(Label(name=LabelType.FRAMEWORK, value='behave'))
@@ -191,6 +194,36 @@ def attach_data(self, body, name, attachment_type, extension):
191194
def attach_file(self, source, name, attachment_type, extension):
192195
self.logger.attach_file(uuid4(), source, name=name, attachment_type=attachment_type, extension=extension)
193196

197+
@allure_commons.hookimpl
198+
def add_description(self, test_description):
199+
test_result = self.logger.get_test(None)
200+
if test_result:
201+
test_result.description = test_description
202+
203+
@allure_commons.hookimpl
204+
def add_description_html(self, test_description_html):
205+
test_result = self.logger.get_test(None)
206+
if test_result:
207+
test_result.descriptionHtml = test_description_html
208+
209+
@allure_commons.hookimpl
210+
def add_link(self, url, link_type, name):
211+
test_result = self.logger.get_test(None)
212+
if test_result:
213+
pattern = u'{}'
214+
if link_type == LinkType.ISSUE and self.issue_pattern:
215+
pattern = self.issue_pattern
216+
elif link_type == LinkType.LINK and self.link_pattern:
217+
pattern = self.link_pattern
218+
219+
link_url = pattern.format(url)
220+
new_link = Link(link_type, link_url, link_url if name is None else name)
221+
for link in test_result.links:
222+
if link.url == new_link.url:
223+
return
224+
225+
test_result.links.append(new_link)
226+
194227

195228
class Context(list):
196229
def __init__(self, _list=list()):

0 commit comments

Comments
 (0)