Skip to content

Refactor Python test XML parsing into focused helpers and add direct parser coverage#2074

Closed
shreejaykurhade wants to merge 3 commits intocodeflash-ai:mainfrom
shreejaykurhade:refactor/parse-test-xml
Closed

Refactor Python test XML parsing into focused helpers and add direct parser coverage#2074
shreejaykurhade wants to merge 3 commits intocodeflash-ai:mainfrom
shreejaykurhade:refactor/parse-test-xml

Conversation

@shreejaykurhade
Copy link
Copy Markdown

This PR refactors the Python test XML parsing flow used by parse_test_xml without changing intended behavior.

codeflash/verification/parse_test_output.py::parse_test_xml is only a thin delegator. The actual large parsing logic lives in codeflash/languages/python/parse_xml.py::parse_python_test_xml, and that function had grown to handle several concerns in one place:

XML loading and validation
failed unittest-loader handling
testcase name extraction
test file and test type resolution
timeout detection
stdout marker pairing
FunctionTestInvocation construction
empty-result logging

This made the parser harder to read, harder to review, and riskier to change.

What Changed

The parser is now split into smaller helpers with clearer responsibilities:

_load_python_test_xml
_is_failed_unittest_loader_case
_log_failed_unittest_loader_output
_extract_test_function_name
_resolve_test_file_path
_resolve_test_type
_did_testcase_time_out
_parse_python_test_case
_build_end_matches
_build_default_test_invocation
_build_marker_test_invocations
_log_empty_test_results

A small internal _ParsedPythonTestCase dataclass was also introduced to carry resolved testcase metadata between steps.

parse_python_test_xml() now acts as the orchestration layer for:

loading the XML
iterating suites/testcases
handling the unittest-loader special case
parsing testcase metadata
building invocations from stdout markers or the default fallback path
logging when no test results were produced

Why This Is Better

This improves maintainability by:

separating testcase resolution from marker parsing
reducing cognitive load in the main parser
localizing future changes to the smallest relevant helper
making the parser easier to test directly at the implementation seam

This is intended to be a behavior-preserving refactor, not a functional redesign.

Tests Added

Added direct parser coverage in tests/test_parse_python_test_xml.py for:

the fallback path when no stdout markers are present
the marker-based path that extracts invocation metadata, runtime, and stdout

These complement the existing higher-level coverage in:

tests/test_test_runner.py
tests/test_parse_test_output_regex.py

Typing

Added the missing return annotation for _parse_func(...) in codeflash/languages/python/parse_xml.py.

Validation

uv run pytest -q tests/test_parse_python_test_xml.py
uv run pytest -q tests/test_test_runner.py
uv run pytest -q tests/test_parse_test_output_regex.py
uv run pytest -q tests/test_parse_python_test_xml.py tests/test_test_runner.py tests/test_parse_test_output_regex.py
uv run ruff check codeflash/languages/python/parse_xml.py tests/test_parse_python_test_xml.py
uv run ruff check codeflash/languages/python/parse_xml.py tests/test_parse_python_test_xml.py --select ANN202

All checks passed. One environment-specific Windows pytest temp-dir cleanup warning still appears on exit, but it does not affect test outcomes.

Risk

Low. This change is a structural refactor of existing Python XML parsing logic with focused direct tests and existing runner-level coverage still passing.

@shreejaykurhade shreejaykurhade force-pushed the refactor/parse-test-xml branch from 7c8eb05 to 8e6b558 Compare April 20, 2026 19:13
@shreejaykurhade shreejaykurhade deleted the refactor/parse-test-xml branch April 20, 2026 19:18
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