TDD in the Era of AI: Red, Green, Refactor, Prompt
Table of Contents
🧪 Is TDD Dead?
Some say that with AI writing code, we don’t need tests. They are wrong. If AI writes the code, you must verify it. And the only way to verify logic at scale is with automated tests.
TDD (Test Driven Development) is the perfect safety net for AI-generated code.
🔄 The New Cycle: Red, Green, Prompt, Refactor
The classic cycle was:
- Red: Write a failing test.
- Green: Write minimum code to pass.
- Refactor: Clean up.
The AI-Enhanced Cycle is:
- Red (Human): You write the test (The Specification). This defines “Done”.
- Green (AI): You ask Copilot/Gemini: “Implement the function to pass this test.”
- Refactor (AI + Human): “Optimize this solution.”
📝 Example: Validation
Step 1: The Test (Human)
@Test
fun `should return error when password lacks uppercase`() {
val validator = PasswordValidator()
val result = validator.validate("password123")
assert(result is ValidationResult.Error)
}
Step 2: The Prompt (Human)
“Generate PasswordValidator that passes this test.”
Step 3: The Code (AI)
class PasswordValidator {
fun validate(input: String): ValidationResult {
if (!input.any { it.isUpperCase() }) {
return ValidationResult.Error("Must contain uppercase")
}
return ValidationResult.Success
}
}
🚀 Why this is faster
- You don’t think about implementation details (“how do I check uppercase in regex?”).
- You only think about Behavior and Edge Cases.
- The AI guarantees the syntax is correct.
- The Test guarantees the logic is correct.
🎯 Conclusion
Don’t let AI write code without a spec. The Test is the Spec. Write the test first, and you will become the Architect of the AI, not just its reviewer.
You might also be interested in
Asynchronous Pair Programming with Agents: Sentinel, Bolt, and Palette in Action
Real Android development cases where delegating to specialized agents beats any standard AI chat. Security, Performance, and UX.
agents.md: The New Standard for AI Development
Discover why agents.md has become the de facto standard for configuring AI agents and how to effectively implement it in Android projects.
AI Skills in Development: Powering Your Android Workflow
Discover how AI Skills transform modern development, automating complex tasks and improving productivity in Android projects.