How to train AI agents to write quality Python tests
Getting coding agents to write good tests isn't just about better prompts—it's about creating the right environment and examples for them to learn from.
- Leverage Python's rich test ecosystem: Python excels for AI-generated tests because pytest patterns are abundant in training data, allowing specific prompts like "use pytest-httpx to mock the endpoints" that agents understand immediately.
- Combat test duplication proactively: The most common anti-pattern is duplicated setup code—push back with specific refactoring commands like "use pytest.mark.parametrize" or "extract common setup into a pytest fixture."
- Seed projects with quality examples: The best strategy is ensuring your project already has clean test patterns—agents automatically mimic existing code quality without extra prompting, similar to how human developers learn from team codebases.
- Use reference repositories as training material: Clone well-tested projects (like datasette/datasette-enrichments) and explicitly tell agents to "imitate the testing patterns it uses"—showing beats telling for AI code generation.
#ai-code-generation#python-testing#pytest-patterns#coding-agents#test-automation
2 articles published
Articles
Sylvain Kerkour
Do nothing, but do it well
There are some days like that where your brain simply refuse to work, and too bad for you but your job relies entierly on your brain. What if instead of