generate_tests
Generate tests for your code. Supports multiple languages and optional frameworks like pytest or Jest.
Instructions
Generates tests for the provided code.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Code to generate tests for. | |
| language | No | Programming language. | python |
| framework | No | Test framework (optional — pytest, jest, unittest, etc.). |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- tools/tests.py:8-42 (handler)Async function that generates tests for provided code. Takes code, language, and optional framework, calls Groq API via the TESTS prompt, caches results, and returns JSON with test cases.
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=False, indent=2) cache.set(key, out) return out - tools/tests.py:9-19 (schema)Docstring/type annotations serving as the schema: accepts code (str), language (str, default python), framework (str, default empty). Returns JSON string.
""" 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. """ - tools/__init__.py:4-4 (registration)Re-exports generate_tests from tools/tests.py so it can be imported as tools.generate_tests.
from .tests import generate_tests - server.py:33-33 (registration)Registers generate_tests as an MCP tool on the FastMCP server.
mcp.tool()(generate_tests)