Back to Repositories

Testing Issue Reference Extraction Functionality in OpenHands

This test suite validates the issue reference extraction functionality in the OpenHands repository. It focuses on testing the IssueHandler class’s ability to parse and identify GitHub issue references in various text contexts and formats.

Test Coverage Overview

The test suite provides comprehensive coverage for issue reference extraction scenarios.

Key functionality tested includes:
  • Basic issue reference detection
  • Multiple issue reference handling
  • Context-aware parsing (code blocks, inline code, URLs)
  • Markdown link processing
Edge cases cover ignored contexts like code blocks and URLs while ensuring proper extraction from markdown links.

Implementation Analysis

The testing approach uses pytest’s assertion framework to verify the _extract_issue_references method behavior. The implementation follows a pattern of isolated test cases, each focusing on a specific parsing scenario.

The tests utilize mock LLMConfig and IssueHandler instances to create a controlled testing environment.

Technical Details

Testing tools and configuration:
  • Python unittest framework
  • Mock LLMConfig configuration
  • IssueHandler class initialization with test parameters
  • String parsing and pattern matching validation

Best Practices Demonstrated

The test suite demonstrates several testing best practices:
  • Isolated test cases for specific functionality
  • Clear test case naming and organization
  • Comprehensive edge case coverage
  • Mock object usage for dependencies
  • Consistent assertion patterns

all-hands-ai/openhands

tests/unit/resolver/test_issue_references.py

            
from openhands.core.config.llm_config import LLMConfig
from openhands.resolver.issue_definitions import IssueHandler


def test_extract_issue_references():
    llm_config = LLMConfig(model='test', api_key='test')
    handler = IssueHandler('test-owner', 'test-repo', 'test-token', llm_config)

    # Test basic issue reference
    assert handler._extract_issue_references('Fixes #123') == [123]

    # Test multiple issue references
    assert handler._extract_issue_references('Fixes #123, #456') == [123, 456]

    # Test issue references in code blocks should be ignored
    assert handler._extract_issue_references("""
    Here's a code block:
    ```python
    # This is a comment with #123
    def func():
        pass  # Another #456
    ```
    But this #789 should be extracted
    """) == [789]

    # Test issue references in inline code should be ignored
    assert handler._extract_issue_references(
        'This `#123` should be ignored but #456 should be extracted'
    ) == [456]

    # Test issue references in URLs should be ignored
    assert handler._extract_issue_references(
        'Check http://example.com/#123 but #456 should be extracted'
    ) == [456]

    # Test issue references in markdown links should be extracted
    assert handler._extract_issue_references(
        '[Link to #123](http://example.com) and #456'
    ) == [123, 456]

    # Test issue references with text around them
    assert handler._extract_issue_references(
        'Issue #123 is fixed and #456 is pending'
    ) == [123, 456]