Phase 1: Test-Driven AI Development · 50 min · Python · pytest
TDD as the Primary Defense Against AI Code Defects
Tests first, code second. Especially when the code is AI-generated.
Hiring signal: Engineers who can explain why TDD is the primary defense against AI code defects -- not just a best practice but a structural necessity given the generation-review asymmetry -- demonstrate strategic understanding of AI code quality. This is what distinguishes an SDET who writes tests from one who designs testing strategies.
What you will learn
- Explain why TDD catches AI defects that post-hoc testing misses
- Describe the spec-first principle and how it constrains AI code generation
- Compare TDD vs traditional testing defect catch rates for AI code
- Apply the psychological benefit of TDD: thinking about edge cases before the AI generates the happy path
The Problem
A developer asks an AI assistant to write a function that validates email addresses. The AI generates:
def validate_email(email):
return "@" in email and "." in email
The developer writes a test:
def test_validate_email():
assert validate_email("user@example.com") == True
The test passes. The code ships. A week later, a user registers with @. as their email -- which passes validation but is obviously invalid. Then someone registers with user@ -- which also passes. The validation function is structurally wrong, but the test was written after the code, so it only tested the happy path that the code already handled.
This is the post-hoc testing trap: when you write tests after the code exists, you unconsciously test what the code does rather than what it should do. You are validating the implementation, not the specification.
TDD inverts this: you write the test first, from the specification, before any code exists. The test encodes what the code should do -- including edge cases the AI would not think of. Then the AI generates code to satisfy the test. If the AI produces return "@" in email and "." in email, the test for validate_email("@.") == False fails, and the AI must try again.
Why TDD Catches What Post-Hoc Testing Misses
The generation-review asymmetry from Phase 0 (78.7% self-identification vs 55.8% generation) means AI is better at finding bugs than at not generating them. TDD exploits this asymmetry:
| Testing approach | When tests are written | What they test | AI defect catch rate |
|---|
| Post-hoc testing | After AI generates code | What the code does (implementation validation) | Low -- tests share the AI's blind spots |
| TDD | Before AI generates code | What the code should do (specification verification) | High -- tests encode requirements the AI doesn't know |
When you write tests after the AI generates code, you are susceptible to the same cognitive biases from Lesson 3: you see the code, form an anchor, and write tests that confirm the code works rather than tests that probe where it fails. When you write tests first, you are forced to think about edge cases, error conditions, and invariants before the AI's happy path anchors your thinking.
TDD exploits the generation-review asymmetry
The 78.7% self-identification rate means AI is good at reviewing code for bugs. When you write tests first and the AI generates code that fails those tests, the AI can then review its own output against the test and fix the failure. This is the AI TDD loop: you define the contract, the AI generates code, the test catches violations, the AI fixes them. You are using the AI's strength (review) to compensate for its weakness (generation).
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Spec-First Principle, TDD vs Post-Hoc: Defect Catch Rate, The Psychological Benefit, Build It — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy