test generation
**Test Generation with LLMs**
**Automated Test Generation**
LLMs can generate unit tests, integration tests, and test data based on code analysis.
**Unit Test Generation**
```python
def generate_tests(function_code: str, language: str) -> str:
return llm.generate(f"""
Generate comprehensive unit tests for this {language} function.
Include:
- Happy path tests
- Edge cases
- Error handling
- Boundary conditions
Function:
```{language}
{function_code}
```
Generate tests using pytest/unittest:
""")
```
## Test Coverage Expansion
```python
def expand_coverage(code: str, existing_tests: str) -> str:
return llm.generate(f"""
Analyze this code and existing tests.
Generate additional tests to improve coverage.
Code:
{code}
Existing tests:
{existing_tests}
Additional tests needed:
""")
```
**Property-Based Test Hints**
```python
def suggest_properties(function_code: str) -> str:
return llm.generate(f"""
Suggest property-based tests (hypothesis-style) for this function.
What invariants should hold?
Function:
{function_code}
Properties to test:
""")
```
**Test Data Generation**
```python
def generate_test_data(schema: str, count: int) -> str:
return llm.generate(f"""
Generate {count} realistic test records matching this schema:
{schema}
Return as JSON array.
""")
```
**Integration with Testing Frameworks**
| Framework | Use Case |
|-----------|----------|
| pytest | Python unit tests |
| Jest | JavaScript testing |
| JUnit | Java testing |
| Hypothesis | Property-based testing |
**Workflow Integration**
```python
# CI/CD integration
def review_test_coverage(pr_code: str, pr_tests: str) -> dict:
return llm.generate(f"""
Evaluate test coverage for this PR.
New code:
{pr_code}
New tests:
{pr_tests}
Assess:
- Are all new functions tested?
- Are edge cases covered?
- Any missing test scenarios?
""")
```
**Limitations**
- Generated tests may have bugs
- May not understand complex business logic
- Could miss important edge cases
- Always review generated tests