When to Write Tests: Before, During, or After
You'll learn: How the timing of writing tests affects your development process and code quality.
The Three Approaches
Writing tests isn't one-size-fits-all. You have three main options for when to create them:
Before coding (Test-Driven Development): You write a failing test first, then write just enough code to make it pass. This forces you to think about design before implementation and guarantees test coverage. However, it requires discipline and can feel slower initially.
During coding (Test-Alongside Development): You alternate between writing a bit of production code and its corresponding tests. This balances design thinking with momentum. You're still getting immediate feedback, but with slightly more flexibility than strict TDD.
After coding (Test-After Development): You implement features first, then add tests. This feels fastest when prototyping or exploring ideas, but risks creating hard-to-test code. You may also forget edge cases or lose motivation to test "finished" code.
The Trade-offs
Think of it like navigation: writing tests before is like planning your route before driving—slower start, but fewer wrong turns. Writing tests after is like driving first and mapping later—fast initially, but you might miss important landmarks or get lost.
None of these approaches is universally "correct." TDD excels when requirements are clear and you want design feedback. Testing-after works for spikes or throwaway prototypes. Testing-alongside offers a practical middle ground for many teams.
The key is choosing consciously based on your context: project maturity, team experience, requirement clarity, and risk tolerance.
Key Takeaway: Tests can be written before (TDD), during, or after implementation—each timing strategy has distinct trade-offs in speed, design quality, and coverage.