generate_tests
Generate test cases and runnable test code for provided code, with support for various languages and test frameworks, including a coverage estimate.
Instructions
Generates tests for the provided code. Args: code: Code to generate tests for. language: Programming language. framework: Test framework (optional - pytest, jest, unittest, etc.). Returns: JSON with test cases, runnable test code, and coverage estimate.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| language | No | python | |
| framework | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/tests.py:6-35 (handler)The main async handler function that generates tests. Takes code, language (default 'python'), and optional framework. Sends a request to Groq LLM with the TESTS prompt, caches the result, and returns JSON with test cases, runnable code, and coverage estimate.
async def generate_tests(code: str, language: str = "python", framework: str = "") -> str: """ Generates tests for the provided code. Args: code: Code to generate tests for. language: Programming language. framework: Test framework (optional - pytest, jest, unittest, etc.). Returns: JSON with test cases, runnable test code, and coverage estimate. """ if not code.strip(): return error_response("Empty code provided.") key = cache.make_key("generate_tests", code, language, framework) if hit := cache.get(key): return hit framework_block = f"\nUse framework: {framework}" if framework else "" user = f"Language: {language}{framework_block}\n\nCode:\n```{language}\n{code}\n```" try: raw = await call(TESTS, user) result = json.loads(raw) except httpx.HTTPStatusError as e: return error_response(f"Groq API error {e.response.status_code}", e.response.text[:300]) except json.JSONDecodeError as e: return error_response("Groq returned invalid JSON", str(e)) except ValueError as e: return error_response(str(e)) out = json.dumps(result, ensure_ascii=True, indent=2) cache.set(key, out) return out - tools/__init__.py:4-11 (registration)Re-export of generate_tests from tools.tests module.
from .tests import generate_tests from .file_tool import analyze_file from .cache_tool import cache_info from .report import generate_report __all__ = [ "analyze_code", "compare_code", "explain_code", "generate_tests", "analyze_file", "cache_info", "generate_report", ]