Skip to content

fix(pytest): properly include nested classes in fullName, historyId, testCaseId, and subSuite #869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions allure-pytest/src/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from itertools import chain, islice
from itertools import chain, islice, repeat
from allure_commons.utils import SafeFormatter, md5
from allure_commons.utils import format_exception, format_traceback
from allure_commons.model2 import Status
Expand Down Expand Up @@ -123,19 +123,29 @@ def allure_name(item, parameters, param_id=None):

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


def ensure_len(value, min_length, fill_value=None):
yield from value
yield from repeat(fill_value, min_length - len(value))


def allure_suite_labels(item):
head, possibly_clazz, tail = islice(chain(item.nodeid.split('::'), [None], [None]), 3)
clazz = possibly_clazz if tail else None
head, *class_names, _ = ensure_len(item.nodeid.split("::"), 2)
file_name, path = islice(chain(reversed(head.rsplit('/', 1)), [None]), 2)
module = file_name.split('.')[0]
package = path.replace('/', '.') if path else None
pairs = dict(zip([LabelType.PARENT_SUITE, LabelType.SUITE, LabelType.SUB_SUITE], [package, module, clazz]))
pairs = dict(
zip(
[LabelType.PARENT_SUITE, LabelType.SUITE, LabelType.SUB_SUITE],
[package, module, " > ".join(class_names)],
),
)
labels = dict(allure_labels(item))
default_suite_labels = []
for label, value in pairs.items():
Expand Down
4 changes: 4 additions & 0 deletions allure-python-commons-test/src/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,7 @@ def with_mode(mode):

def has_history_id(matcher=None):
return has_entry('historyId', matcher or anything())


def has_full_name(matcher):
return has_entry("fullName", matcher)
47 changes: 47 additions & 0 deletions tests/allure_pytest/defects/issue868_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import allure
from hamcrest import assert_that, is_not
from tests.allure_pytest.pytest_runner import AllurePytestRunner

from allure_commons_test.report import has_test_case
from allure_commons_test.result import has_full_name
from allure_commons_test.label import has_sub_suite


@allure.issue("868", name="Issue 868")
def test_nested_class_affects_fullname_and_subsuite(allure_pytest_runner: AllurePytestRunner):
"""
>>> class TestFoo:
... class TestBar:
... def test_bar(self):
... pass
"""

allure_results = allure_pytest_runner.run_docstring(filename="foo_test.py")

assert_that(
allure_results,
has_test_case(
"test_bar",
has_full_name("foo_test.TestFoo.TestBar#test_bar"),
has_sub_suite("TestFoo > TestBar"),
),
)


@allure.issue("868", name="Issue 868")
def test_nested_class_affects_testcaseid_and_historyid(allure_pytest_runner: AllurePytestRunner):
"""
>>> class TestFoo:
... class TestFoo:
... def test_foo(self):
... pass
... def test_foo(self):
... pass
"""

allure_results = allure_pytest_runner.run_docstring(filename="foo_test.py")
test_case_id1, test_case_id2 = [tc["testCaseId"] for tc in allure_results.test_cases]
history_id1, history_id2 = [tc["historyId"] for tc in allure_results.test_cases]

assert_that(test_case_id1, is_not(test_case_id2))
assert_that(history_id1, is_not(history_id2))