When to Write Tests
You'll learn: The three main strategies for when to create tests in the development cycle, and how timing affects code design and confidence.
Core Idea
Testing isn't just what you test—it's also when. You have three fundamental approaches:
Test-First (TDD)
Write tests before implementation code. This forces you to think about requirements and interfaces upfront. Like designing a key before cutting the lock, you specify the contract your code must fulfill, then make it work.
Advantages: Drives better design (testable code tends to be well-structured), ensures every feature has tests, clarifies requirements immediately.
Test-During (Concurrent)
Write tests alongside production code. You implement a small piece of functionality, then immediately test it before moving on. Think of checking your recipe step-by-step while cooking.
Advantages: Natural workflow for many developers, catches bugs quickly while context is fresh, balances design thinking with implementation momentum.
Test-After (Post-Implementation)
Write tests after code is working. This is common when exploring solutions or when requirements are unclear. Like writing instructions after assembling furniture to document what you learned.
Advantages: Useful for prototyping, handles unclear requirements, can validate existing legacy code.
Which Strategy to Choose?
- Critical features or bug fixes? Test-first ensures correctness.
- Exploratory work or spikes? Test-after lets you experiment freely.
- Normal feature development? Test-during often provides the best balance.
Many teams mix strategies based on the situation. The goal is always the same: ensure your code is verified through test cases with appropriate coverage using techniques like equivalence partitioning and boundary value analysis.
Key Takeaway: When you write tests shapes how you design code—test-first improves architecture, test-during maintains momentum, and test-after documents existing behavior.