Skip to content

Commit 14624e4

Browse files
authored
Fix suite label duplication when using dynamic suite functions (Fixes #586) (#746)
1 parent 12085cd commit 14624e4

File tree

3 files changed

+89
-10
lines changed

3 files changed

+89
-10
lines changed
Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
Custom suite
22
____________
33

4+
Use the `@allure.parent_suite`, `@allure.suite`, or `@allure.sub_suite`
5+
decorators to overwrite default suite labels:
46

57
>>> import allure
68

7-
>>> @allure.parent_suite("parent suite name")
8-
>>> @allure.suite("suite name")
9-
>>> @allure.sub_suite("sub suite name")
10-
... def test_custom_suite():
11-
... pass
9+
>>> class TestCustomSuites:
10+
... @allure.parent_suite("parent suite name")
11+
... @allure.suite("suite name")
12+
... @allure.sub_suite("sub suite name")
13+
... def test_custom_suites(self):
14+
... pass
1215

1316

17+
Use the `allure.dynamic.parent_suite`, `allure.dynamic.suite`, or
18+
`allure.dynamic.sub_suite` functions to overwrite default suite labels
19+
dynamically:
20+
21+
>>> import allure
22+
23+
>>> class TestCustomDynamicSuites:
24+
... def test_custom_dynamic_suites(self):
25+
... allure.dynamic.parent_suite("parent suite name")
26+
... allure.dynamic.suite("suite name")
27+
... allure.dynamic.sub_suite("sub suite name")

allure-pytest/src/listener.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
class AllureListener:
2929

30+
SUITE_LABELS = {
31+
LabelType.PARENT_SUITE,
32+
LabelType.SUITE,
33+
LabelType.SUB_SUITE,
34+
}
35+
3036
def __init__(self, config):
3137
self.config = config
3238
self.allure_logger = AllureReporter()
@@ -128,7 +134,7 @@ def pytest_runtest_teardown(self, item):
128134
test_result = self.allure_logger.get_test(uuid)
129135
test_result.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)])
130136
test_result.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)])
131-
test_result.labels.extend([Label(name=name, value=value) for name, value in allure_suite_labels(item)])
137+
self.__apply_default_suites(item, test_result)
132138
test_result.labels.append(Label(name=LabelType.HOST, value=self._host))
133139
test_result.labels.append(Label(name=LabelType.THREAD, value=self._thread))
134140
test_result.labels.append(Label(name=LabelType.FRAMEWORK, value='pytest'))
@@ -284,6 +290,19 @@ def add_parameter(self, name, value, excluded, mode: ParameterMode):
284290
test_result.parameters.append(Parameter(name=name, value=represent(value),
285291
excluded=excluded or None, mode=mode.value if mode else None))
286292

293+
def __apply_default_suites(self, item, test_result):
294+
default_suites = allure_suite_labels(item)
295+
existing_suites = {
296+
label.name
297+
for label in test_result.labels
298+
if label.name in AllureListener.SUITE_LABELS
299+
}
300+
test_result.labels.extend(
301+
Label(name=name, value=value)
302+
for name, value in default_suites
303+
if name not in existing_suites
304+
)
305+
287306

288307
class ItemCache:
289308

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,67 @@
11
""" ./allure-pytest/examples/label/suite/custom_suite.rst """
22

3-
from hamcrest import assert_that
3+
from hamcrest import assert_that, not_
44
from tests.allure_pytest.pytest_runner import AllurePytestRunner
55

6+
import allure
67
from allure_commons_test.report import has_test_case
78
from allure_commons_test.label import has_suite, has_parent_suite, has_sub_suite
89

910

1011
def test_custom_suite(allure_pytest_runner: AllurePytestRunner):
11-
allure_results = allure_pytest_runner.run_docpath_examples()
12+
allure_results = allure_pytest_runner.run_docpath_examples(cache=True)
1213

1314
assert_that(
1415
allure_results,
1516
has_test_case(
16-
"test_custom_suite",
17+
"TestCustomSuites#test_custom_suites",
1718
has_suite("suite name"),
1819
has_parent_suite("parent suite name"),
19-
has_sub_suite("sub suite name")
20+
has_sub_suite("sub suite name"),
21+
not_(
22+
has_parent_suite(
23+
not_("parent suite name")
24+
)
25+
),
26+
not_(
27+
has_suite(
28+
not_("suite name")
29+
)
30+
),
31+
not_(
32+
has_sub_suite(
33+
not_("sub suite name")
34+
)
35+
)
36+
)
37+
)
38+
39+
40+
@allure.issue("586", "Issue 586")
41+
def test_custom_dynamic_suites(allure_pytest_runner: AllurePytestRunner):
42+
allure_results = allure_pytest_runner.run_docpath_examples(cache=True)
43+
44+
assert_that(
45+
allure_results,
46+
has_test_case(
47+
"TestCustomDynamicSuites#test_custom_dynamic_suites",
48+
has_suite("suite name"),
49+
has_parent_suite("parent suite name"),
50+
has_sub_suite("sub suite name"),
51+
not_(
52+
has_parent_suite(
53+
not_("parent suite name")
54+
)
55+
),
56+
not_(
57+
has_suite(
58+
not_("suite name")
59+
)
60+
),
61+
not_(
62+
has_sub_suite(
63+
not_("sub suite name")
64+
)
65+
)
2066
)
2167
)

0 commit comments

Comments
 (0)