Skip to content

Conversation

@Sgitario
Copy link

@Sgitario Sgitario commented Dec 17, 2025

Description

When using pytest-xdist with --dist=loadgroup, tests with xdist_group markers
get @group_name appended to their nodeid (e.g., test_foo@my_group).

For parameterized tests, item.originalname is available and correctly used.
For non-parameterized tests, item.originalname is None, so the code falls
back to item.name which includes the @suffix. This produces an invalid code_ref
like path/file.py:test_foo@group, causing the test to not appear in ReportPortal.

Note that appending the @group_name to the test name is done on purpose in tests with xdist_group, see for further details in here.

Comparison with pytest-ibutsu

The pytest-ibutsu plugin handles this
correctly by using item.location[2] which returns the original function name
without any xdist suffixes:

pytest-ibutsu/src/pytest_ibutsu/modeling.py:

@staticmethod
def _get_test_idents(item: pytest.Item) -> str:
    try:
        return item.location[2]
    except AttributeError:
        ...

This PR aligns pytest-reportportal behavior with pytest-ibutsu.

Solution

Add _get_method_name() method that:

  1. Returns item.originalname if available
  2. Otherwise strips trailing @suffix from item.name
  3. Preserves @ inside parameter brackets (e.g., test_email[user@example.com])

Summary by CodeRabbit

Release Notes

  • Refactor

    • Improved test method name extraction with enhanced handling of parametrized tests and special characters.
  • Tests

    • Added unit tests for method name resolution logic.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 17, 2025

Walkthrough

A helper method _get_method_name is extracted to centralize test method name resolution logic in the PyTestService class. The method handles name preference (originalname over name), trailing @ suffix stripping, and preservation of @ characters in parameter lists. Corresponding unit tests cover edge cases.

Changes

Cohort / File(s) Summary
Method extraction
pytest_reportportal/service.py
Introduces _get_method_name(item: Item) -> str helper method to derive test method names by preferring originalname when available, otherwise stripping trailing @ suffix while preserving @ inside brackets. Refactors _get_code_ref to use this helper instead of inline logic.
Test coverage
tests/unit/test_service.py
Adds unit tests for _get_method_name validating: originalname preference, name preservation when originalname is None, trailing @ stripping, and @ preservation inside parameter lists.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Straightforward extraction of name-resolution logic into a dedicated helper
  • Clear method contract with specific parameter handling rules
  • Test cases cover distinct edge cases without introducing complex assertions
  • Limited scope affecting only 2 files with consistent, repeatable patterns

Poem

🐰 A method name, once scattered wide,
Now nested in a helper's pride.
@ symbols dance, but trapped they stay,
When brackets guide them on their way.
Refactored clean, the tests align,
One helper method, by design.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: introducing logic to strip trailing @suffix from method names in the _get_code_ref flow, which directly addresses the PR's core objective.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/unit/test_service.py (1)

31-72: Comprehensive test coverage for the new helper method.

The four test cases effectively validate all the key behaviors:

  • Regular names pass through unchanged
  • originalname takes precedence when present
  • Trailing @suffix is correctly stripped
  • @ symbols inside parameter brackets are preserved

Optional enhancement: Consider adding a test for the combined edge case where both parameter brackets with @ and a trailing suffix are present:

def test_get_method_name_params_with_suffix(mocked_item, rp_service):
    """Test @ in params is preserved while trailing suffix is stripped."""
    mocked_item.name = "test_email[user@example.com]@group"
    mocked_item.originalname = None
    
    result = rp_service._get_method_name(mocked_item)
    
    expect(result == "test_email[user@example.com]")
    assert_expectations()

This would provide explicit confirmation that the logic handles the most complex real-world scenario from pytest-xdist with loadgroup.

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5b84bff and 84ea161.

📒 Files selected for processing (2)
  • pytest_reportportal/service.py (2 hunks)
  • tests/unit/test_service.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/unit/test_service.py (2)
tests/unit/conftest.py (2)
  • mocked_item (95-104)
  • rp_service (108-113)
pytest_reportportal/service.py (1)
  • _get_method_name (370-393)
🔇 Additional comments (2)
pytest_reportportal/service.py (2)

370-393: Excellent implementation of the suffix-stripping logic!

The method correctly handles all the edge cases described in the PR objectives:

  • Prefers originalname when available (for parameterized tests)
  • Strips trailing @suffix added by pytest-xdist's loadgroup
  • Preserves @ characters inside parameter brackets (e.g., test_email[user@example.com])

The use of rfind to locate the last bracket and last @ ensures that only trailing suffixes are removed, while @ symbols within test parameters remain intact.


612-612: Good refactoring to use the centralized helper.

Replacing the inline originalname check with _get_method_name(item) improves maintainability and ensures consistent name resolution logic.

@Sgitario
Copy link
Author

Hello @HardNorth , can you take a look into these changes? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant