Skip to content

Commit a594ff2

Browse files
misrasaurabh1claude
andcommitted
feat: add JUnit 4/TestNG support for Java test framework detection
- Update TestConfig._detect_java_test_framework() to check parent pom.xml for multi-module projects where test deps are in a different module - Add framework aliases in registry to map junit4/testng to Java support - Correctly detect JUnit 4 projects and send correct framework to AI service Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3fdebd3 commit a594ff2

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

codeflash/languages/registry.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,20 @@ def get_language_support_by_framework(test_framework: str) -> LanguageSupport |
201201
if test_framework in _FRAMEWORK_CACHE:
202202
return _FRAMEWORK_CACHE[test_framework]
203203

204+
# Map of frameworks that should use the same language support
205+
# All Java test frameworks (junit4, junit5, testng) use the Java language support
206+
framework_aliases = {
207+
"junit4": "junit5", # JUnit 4 uses Java support (which reports junit5 as primary)
208+
"testng": "junit5", # TestNG also uses Java support
209+
}
210+
211+
# Use the canonical framework name for lookup
212+
lookup_framework = framework_aliases.get(test_framework, test_framework)
213+
204214
# Search all registered languages for one with matching test framework
205215
for language in _LANGUAGE_REGISTRY:
206216
support = get_language_support(language)
207-
if hasattr(support, "test_framework") and support.test_framework == test_framework:
217+
if hasattr(support, "test_framework") and support.test_framework == lookup_framework:
208218
_FRAMEWORK_CACHE[test_framework] = support
209219
return support
210220

codeflash/verification/verification_utils.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,46 @@ class TestConfig:
114114
def test_framework(self) -> str:
115115
"""Returns the appropriate test framework based on language.
116116
117-
Returns 'jest' for JavaScript/TypeScript, 'junit5' for Java, 'pytest' for Python (default).
117+
Returns 'jest' for JavaScript/TypeScript, detected JUnit version for Java, 'pytest' for Python (default).
118118
"""
119119
if is_javascript():
120120
return "jest"
121121
if is_java():
122-
return "junit5"
122+
return self._detect_java_test_framework()
123123
return "pytest"
124124

125+
def _detect_java_test_framework(self) -> str:
126+
"""Detect the Java test framework from the project configuration.
127+
128+
Returns 'junit4', 'junit5', or 'testng' based on project dependencies.
129+
Checks both the project root and parent directories for multi-module projects.
130+
Defaults to 'junit5' if detection fails.
131+
"""
132+
try:
133+
from codeflash.languages.java.config import detect_java_project
134+
135+
# First try the project root
136+
config = detect_java_project(self.project_root_path)
137+
if config and config.test_framework and (config.has_junit4 or config.has_junit5 or config.has_testng):
138+
return config.test_framework
139+
140+
# For multi-module projects, check parent directories
141+
current = self.project_root_path.parent
142+
while current != current.parent:
143+
pom_path = current / "pom.xml"
144+
if pom_path.exists():
145+
parent_config = detect_java_project(current)
146+
if parent_config and (parent_config.has_junit4 or parent_config.has_junit5 or parent_config.has_testng):
147+
return parent_config.test_framework
148+
current = current.parent
149+
150+
# Return whatever the initial detection found, or default
151+
if config and config.test_framework:
152+
return config.test_framework
153+
except Exception:
154+
pass
155+
return "junit5" # Default fallback
156+
125157
def set_language(self, language: str) -> None:
126158
"""Set the language for this test config.
127159

0 commit comments

Comments
 (0)