Skip to content

Commit 0f7da56

Browse files
authored
fix(pytest): properly include nested classes in fullName, historyId, testCaseId, and subSuite (allure-framework#869)
1 parent 4d67928 commit 0f7da56

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

allure-pytest/src/utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from itertools import chain, islice
2+
from itertools import chain, islice, repeat
33
from allure_commons.utils import SafeFormatter, md5
44
from allure_commons.utils import format_exception, format_traceback
55
from allure_commons.model2 import Status
@@ -123,19 +123,29 @@ def allure_name(item, parameters, param_id=None):
123123

124124
def allure_full_name(item: pytest.Item):
125125
package = allure_package(item)
126-
class_name = f".{item.parent.name}" if isinstance(item.parent, pytest.Class) else ''
126+
class_names = item.nodeid.split("::")[1:-1]
127+
class_part = ("." + ".".join(class_names)) if class_names else ""
127128
test = item.originalname if isinstance(item, pytest.Function) else item.name.split("[")[0]
128-
full_name = f'{package}{class_name}#{test}'
129+
full_name = f'{package}{class_part}#{test}'
129130
return full_name
130131

131132

133+
def ensure_len(value, min_length, fill_value=None):
134+
yield from value
135+
yield from repeat(fill_value, min_length - len(value))
136+
137+
132138
def allure_suite_labels(item):
133-
head, possibly_clazz, tail = islice(chain(item.nodeid.split('::'), [None], [None]), 3)
134-
clazz = possibly_clazz if tail else None
139+
head, *class_names, _ = ensure_len(item.nodeid.split("::"), 2)
135140
file_name, path = islice(chain(reversed(head.rsplit('/', 1)), [None]), 2)
136141
module = file_name.split('.')[0]
137142
package = path.replace('/', '.') if path else None
138-
pairs = dict(zip([LabelType.PARENT_SUITE, LabelType.SUITE, LabelType.SUB_SUITE], [package, module, clazz]))
143+
pairs = dict(
144+
zip(
145+
[LabelType.PARENT_SUITE, LabelType.SUITE, LabelType.SUB_SUITE],
146+
[package, module, " > ".join(class_names)],
147+
),
148+
)
139149
labels = dict(allure_labels(item))
140150
default_suite_labels = []
141151
for label, value in pairs.items():

allure-python-commons-test/src/result.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,7 @@ def with_mode(mode):
224224

225225
def has_history_id(matcher=None):
226226
return has_entry('historyId', matcher or anything())
227+
228+
229+
def has_full_name(matcher):
230+
return has_entry("fullName", matcher)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import allure
2+
from hamcrest import assert_that, is_not
3+
from tests.allure_pytest.pytest_runner import AllurePytestRunner
4+
5+
from allure_commons_test.report import has_test_case
6+
from allure_commons_test.result import has_full_name
7+
from allure_commons_test.label import has_sub_suite
8+
9+
10+
@allure.issue("868", name="Issue 868")
11+
def test_nested_class_affects_fullname_and_subsuite(allure_pytest_runner: AllurePytestRunner):
12+
"""
13+
>>> class TestFoo:
14+
... class TestBar:
15+
... def test_bar(self):
16+
... pass
17+
"""
18+
19+
allure_results = allure_pytest_runner.run_docstring(filename="foo_test.py")
20+
21+
assert_that(
22+
allure_results,
23+
has_test_case(
24+
"test_bar",
25+
has_full_name("foo_test.TestFoo.TestBar#test_bar"),
26+
has_sub_suite("TestFoo > TestBar"),
27+
),
28+
)
29+
30+
31+
@allure.issue("868", name="Issue 868")
32+
def test_nested_class_affects_testcaseid_and_historyid(allure_pytest_runner: AllurePytestRunner):
33+
"""
34+
>>> class TestFoo:
35+
... class TestFoo:
36+
... def test_foo(self):
37+
... pass
38+
... def test_foo(self):
39+
... pass
40+
"""
41+
42+
allure_results = allure_pytest_runner.run_docstring(filename="foo_test.py")
43+
test_case_id1, test_case_id2 = [tc["testCaseId"] for tc in allure_results.test_cases]
44+
history_id1, history_id2 = [tc["historyId"] for tc in allure_results.test_cases]
45+
46+
assert_that(test_case_id1, is_not(test_case_id2))
47+
assert_that(history_id1, is_not(history_id2))

0 commit comments

Comments
 (0)