Back to agents
Agent #3
✅ Test Automation & Generation Agent
This agent generates comprehensive unit tests, integration tests, and end-to-end tests from code. It analyzes functions, classes, and modules to create test cases covering normal flow, edge cases, and error scenarios. python 03_test_generation.py /path/to/file.py python 03_test_generation.py "class UserManager"
Key Features
- ✓Unit test generation
- ✓Edge case coverage
- ✓Error scenario testing
- ✓Pytest framework integration
- ✓90%+ code coverage target
Implementation
Download .py03_test_generation.py
#!/usr/bin/env python3
"""
Test Automation & Generation Agent
This agent generates comprehensive unit tests, integration tests, and
end-to-end tests from code. It analyzes functions, classes, and modules
to create test cases covering normal flow, edge cases, and error scenarios.
Usage:
python 03_test_generation.py /path/to/file.py
python 03_test_generation.py "class UserManager"
"""
import sys
from anthropic import Anthropic
def main():
if len(sys.argv) < 2:
print("Usage: python 03_test_generation.py <file_path_or_code>")
sys.exit(1)
target = " ".join(sys.argv[1:])
client = Anthropic()
system_prompt = """You are a test automation expert. Your job is to:
1. Analyze code structure and logic
2. Identify all code paths and edge cases
3. Generate comprehensive test cases
4. Cover happy path, error cases, and edge conditions
5. Use pytest and mock libraries appropriately
6. Ensure tests are independent and deterministic
Provide tests that increase code coverage to 90%+ and document test intent."""
task = f"""Generate comprehensive tests for:
{target}
Create:
1. Unit tests for individual functions/methods
2. Integration tests for related components
3. Edge case and error scenario tests
4. Test fixtures and setup/teardown
5. Comments explaining test purpose
Use pytest framework. Include assertions that verify:
- Correct return values
- Side effects and state changes
- Exception handling
- Boundary conditions"""
print("✅ Starting Test Generation Agent...\n")
print("=" * 60)
try:
with client.messages.stream(
model="claude-opus-4-7",
max_tokens=3000,
system=system_prompt,
messages=[{"role": "user", "content": task}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print("\n" + "=" * 60)
print("\n✅ Test generation complete!")
except Exception as e:
print(f"\n❌ Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
How to Use
This agent is designed to be run from the command line:
python 03_test_generation.py <input>The agent will stream its response to the console as it works through the task. Use Ctrl+C to interrupt at any time.
Quick Download
03_test_generation.pyFile
03_test_generation.py
Language
Python 3.8+
Dependencies
anthropic
Setup
- 1. Install:
pip install anthropic - 2. Download the .py file
- 3. Set ANTHROPIC_API_KEY
- 4. Run:
python 03_test_generation.py