Skip to content

Commit eb3d51a

Browse files
fix: prevent duplicate and wrong test-to-function associations in Java
Fixed two critical bugs in Java test discovery that caused incorrect test-to-function mappings: ## Bug 1: Duplicate Test Associations **Problem**: The function_map contained duplicate keys (both func.name and func.qualified_name pointing to the same object). When iterating over the map in Strategy 1, each function was processed twice, causing duplicate test associations. **Example**: - function_map['fibonacci'] → fibonacci function - function_map['Calculator.fibonacci'] → fibonacci function (same object!) When matching testFibonacci, it would match TWICE and get added TWICE. **Fix**: Added duplicate check in Strategy 1 (line 117): ```python if func_info.qualified_name not in matched: matched.append(func_info.qualified_name) ``` ## Bug 2: Wrong Test Associations **Problem**: Strategy 3 (class naming convention) was too broad. It would associate ALL methods in a class with EVERY test in that class's test file. **Example**: - CalculatorTest has testFibonacci and testSumRange - Strategy 3 strips "Test" → "Calculator" - Finds ALL methods in Calculator class (fibonacci, sumRange) - Associates BOTH with EVERY test Result: - testFibonacci incorrectly associated with sumRange - testSumRange incorrectly associated with fibonacci **Fix**: Made Strategy 3 a fallback - only runs if no matches found yet: ```python if not matched and test_method.class_name: ``` ## Impact **Before**: ``` Calculator.fibonacci → 3 tests: - testFibonacci - testFibonacci (duplicate!) - testSumRange (wrong!) Calculator.sumRange → 3 tests: - testFibonacci (wrong!) - testSumRange - testSumRange (duplicate!) ``` **After**: ``` Calculator.fibonacci → 1 test: - testFibonacci ✓ Calculator.sumRange → 1 test: - testSumRange ✓ ``` ## Testing ✅ All 24 test discovery tests pass ✅ Verified with real Java project (java-test-project) ✅ Each test now correctly maps to only its target function This fix is critical for optimization correctness - wrong test associations would cause incorrect behavior verification and benchmarking results. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 7fc14fd commit eb3d51a

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

codeflash/languages/java/test_discovery.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def _match_test_to_functions(
115115

116116
for func_name, func_info in function_map.items():
117117
if func_info.name.lower() in test_name_lower:
118-
matched.append(func_info.qualified_name)
118+
if func_info.qualified_name not in matched:
119+
matched.append(func_info.qualified_name)
119120

120121
# Strategy 2: Method call analysis
121122
# Look for direct method calls in the test code
@@ -137,9 +138,10 @@ def _match_test_to_functions(
137138
if qualified not in matched:
138139
matched.append(qualified)
139140

140-
# Strategy 3: Test class naming convention
141+
# Strategy 3: Test class naming convention (fallback only)
141142
# e.g., CalculatorTest tests Calculator, TestCalculator tests Calculator
142-
if test_method.class_name:
143+
# Only use this if no matches found yet (too broad otherwise)
144+
if not matched and test_method.class_name:
143145
# Remove "Test/Tests" suffix or "Test" prefix
144146
source_class_name = test_method.class_name
145147
if source_class_name.endswith("Tests"):

0 commit comments

Comments
 (0)