Skip to content
Open
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
31 changes: 26 additions & 5 deletions pytest_reportportal/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,31 @@ def _get_scenario_template(self, scenario: Scenario) -> Optional[ScenarioTemplat
if scenario_template and isinstance(scenario_template, ScenarioTemplate):
return scenario_template

def _get_method_name(self, item: Item) -> str:
"""Get the original test method name.

Returns item.originalname if available,
otherwise strips any trailing @suffix from item.name while
preserving @ inside parameter brackets.

:param item: pytest.Item
:return: original method name
"""
if hasattr(item, "originalname") and item.originalname is not None:
return item.originalname

name = item.name
if "@" not in name:
return name

last_bracket = name.rfind("]")
at_pos = name.rfind("@")

if at_pos > last_bracket:
return name[:at_pos]

return name

def _generate_names(self, test_tree: dict[str, Any]) -> None:
if test_tree["type"] == LeafType.ROOT:
test_tree["name"] = "root"
Expand Down Expand Up @@ -584,11 +609,7 @@ def _get_code_ref(self, item: Item) -> str:
# same path on different systems and do not affect Test Case ID on
# different systems
path = os.path.relpath(str(item.fspath), ROOT_DIR).replace("\\", "/")
method_name = (
item.originalname
if hasattr(item, "originalname") and getattr(item, "originalname") is not None
else item.name
)
method_name = self._get_method_name(item)
parent = item.parent
classes = [method_name]
while not isinstance(parent, Module):
Expand Down
44 changes: 44 additions & 0 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,47 @@ def test_get_item_parameters(mocked_item, rp_service):
expect(rp_service._get_parameters(mocked_item) is None)

assert_expectations()


def test_get_method_name_regular(mocked_item, rp_service):
"""Test that regular test names are returned as-is."""
mocked_item.name = "test_simple_function"
mocked_item.originalname = None

result = rp_service._get_method_name(mocked_item)

expect(result == "test_simple_function")
assert_expectations()


def test_get_method_name_uses_originalname(mocked_item, rp_service):
"""Test that originalname is preferred when available."""
mocked_item.name = "test_verify_data[Daily]@sync_group"
mocked_item.originalname = "test_verify_data"

result = rp_service._get_method_name(mocked_item)

expect(result == "test_verify_data")
assert_expectations()


def test_get_method_name_strips_suffix(mocked_item, rp_service):
"""Test that trailing @suffix is stripped when originalname is None."""
mocked_item.name = "test_export_data@data_export"
mocked_item.originalname = None

result = rp_service._get_method_name(mocked_item)

expect(result == "test_export_data")
assert_expectations()


def test_get_method_name_preserves_at_inside_params(mocked_item, rp_service):
"""Test that @ inside parameter brackets is preserved."""
mocked_item.name = "test_email[user@example.com]"
mocked_item.originalname = None

result = rp_service._get_method_name(mocked_item)

expect(result == "test_email[user@example.com]")
assert_expectations()