Skip to content

Commit 6d95702

Browse files
authored
fix dynamic labels and links (fixes #170 via #173)
1 parent 846b5c1 commit 6d95702

File tree

6 files changed

+79
-14
lines changed

6 files changed

+79
-14
lines changed

allure-behave/src/listener.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,10 @@ def stop_test(self, parent_uuid, uuid, name, context, exc_type, exc_val, exc_tb)
105105
self.flush_steps()
106106
status = scenario_status(scenario)
107107
status_details = scenario_status_details(scenario)
108-
self.logger.update_test(uuid, stop=now(), status=status, statusDetails=status_details)
108+
test_result = self.logger.get_test(uuid)
109+
test_result.stop = now()
110+
test_result.status = status
111+
test_result.statusDetails = status_details
109112
self.logger.close_test(uuid)
110113
self.current_step_uuid = None
111114

allure-pytest/src/listener.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def pytest_runtest_protocol(self, item, nextitem):
7373
yield
7474

7575
for name, value in item.callspec.params.items() if hasattr(item, 'callspec') else ():
76-
self.allure_logger.update_test(uuid, parameters=Parameter(name, represent(value)))
76+
test_result = self.allure_logger.get_test(uuid)
77+
if test_result:
78+
test_result.parameters.append(Parameter(name, represent(value)))
7779

7880
test_case.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
7981
test_case.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)])
@@ -94,11 +96,14 @@ def pytest_runtest_protocol(self, item, nextitem):
9496
@pytest.hookimpl(hookwrapper=True)
9597
def pytest_runtest_call(self, item):
9698
uuid = self._cache.get(item.nodeid)
97-
self.allure_logger.update_test(uuid, start=now())
99+
test_result = self.allure_logger.get_test(uuid)
100+
if test_result:
101+
test_result.start = now()
98102

99103
yield
100104

101-
self.allure_logger.update_test(uuid, stop=now())
105+
if test_result:
106+
test_result.stop = now()
102107

103108
@pytest.hookimpl(hookwrapper=True)
104109
def pytest_fixture_setup(self, fixturedef, request):
@@ -170,10 +175,13 @@ def pytest_runtest_makereport(self, item, call):
170175
if report.failed and status == Status.PASSED:
171176
status = Status.BROKEN
172177

173-
if status_details:
174-
self.allure_logger.update_test(uuid, status=status, statusDetails=status_details)
175-
else:
176-
self.allure_logger.update_test(uuid, status=status)
178+
test_result = self.allure_logger.get_test(uuid)
179+
if test_result:
180+
if status_details:
181+
test_result.status = status
182+
test_result.statusDetails = status_details
183+
else:
184+
test_result.status = status
177185

178186
@allure_commons.hookimpl
179187
def attach_data(self, body, name, attachment_type, extension):
@@ -185,12 +193,15 @@ def attach_file(self, source, name, attachment_type, extension):
185193

186194
@allure_commons.hookimpl
187195
def add_link(self, url, link_type, name):
188-
self.allure_logger.update_test(None, links=Link(link_type, url, name))
196+
test_result = self.allure_logger.get_test(None)
197+
if test_result:
198+
test_result.links.append(Link(link_type, url, name))
189199

190200
@allure_commons.hookimpl
191201
def add_label(self, label_type, labels):
192-
for label in labels:
193-
self.allure_logger.update_test(None, labels=Label(label_type, label))
202+
test_result = self.allure_logger.get_test(None)
203+
for label in labels if test_result else ():
204+
test_result.labels.append(Label(label_type, label))
194205

195206

196207
class ItemCache(object):
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import allure
2+
import pytest
3+
4+
5+
@allure.feature('first feature')
6+
def test_dynamic_feature():
7+
"""
8+
>>> allure_report = getfixture('allure_report')
9+
>>> assert_that(allure_report,
10+
... has_test_case('test_dynamic_feature',
11+
... has_feature('first feature'),
12+
... has_feature('second feature')
13+
... )
14+
... )
15+
"""
16+
allure.dynamic.feature('second feature')
17+
18+
19+
@pytest.mark.parametrize('feature', ['first feature', 'second feature'])
20+
def test_parametrized_dynamic_feature(feature):
21+
"""
22+
>>> allure_report = getfixture('allure_report')
23+
>>> for feature in ['first feature', 'second feature']:
24+
... assert_that(allure_report,
25+
... has_test_case('test_parametrized_dynamic_feature[{feature}]'.format(feature=feature),
26+
... has_feature(feature)
27+
... )
28+
... )
29+
"""
30+
allure.dynamic.feature(feature)

allure-pytest/test/links/dynamic_links_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
import allure
23

34
LINK_1 = "https://github.com"
@@ -15,3 +16,17 @@ def test_dynamic_links():
1516
... ))
1617
"""
1718
allure.dynamic.link(LINK_2)
19+
20+
21+
@pytest.mark.parametrize('link', (LINK_1, LINK_2))
22+
def test_parametrized_dynamic_links(link):
23+
"""
24+
>>> allure_report = getfixture('allure_report')
25+
>>> for link in [LINK_1, LINK_2]:
26+
... assert_that(allure_report,
27+
... has_test_case('test_parametrized_dynamic_links[{link}]'.format(link=link),
28+
... has_link(link)
29+
... )
30+
... )
31+
"""
32+
allure.dynamic.link(link)

allure-pytest/tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ commands =
4444
# `tox -e demo -- -k test_single_feature_label` or
4545
# `tox -e demo -- ./test/steps/`
4646
[testenv:demo]
47-
basepython = python3.5
47+
#basepython = python3.5
4848

4949
passenv = HOME
5050

allure-python-commons/src/reporter.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from allure_commons.types import AttachmentType
44
from allure_commons.model2 import ExecutableItem
5+
from allure_commons.model2 import TestResult
56
from allure_commons.model2 import Attachment, ATTACHMENT_PATTERN
67
from allure_commons.utils import now
78
from allure_commons._core import plugin_manager
@@ -28,6 +29,11 @@ def _last_executable(self):
2829
def get_item(self, uuid):
2930
return self._items.get(uuid)
3031

32+
def get_last_item(self, item_type):
33+
for _uuid in reversed(self._items):
34+
if type(self._items[_uuid]) == item_type:
35+
return self._items.get(_uuid)
36+
3137
def start_group(self, uuid, group):
3238
self._items[uuid] = group
3339

@@ -59,8 +65,8 @@ def stop_after_fixture(self, uuid, **kwargs):
5965
def schedule_test(self, uuid, test_case):
6066
self._items[uuid] = test_case
6167

62-
def update_test(self, uuid, **kwargs):
63-
self._update_item(uuid, **kwargs)
68+
def get_test(self, uuid):
69+
return self.get_item(uuid) if uuid else self.get_last_item(TestResult)
6470

6571
def close_test(self, uuid):
6672
test_case = self._items.pop(uuid)

0 commit comments

Comments
 (0)