6 Dec 2007

Why do I need to write the tests first?

When adopting practices like Test Driven Development, developers often ask why they need to write the tests first. Why can't they just write the tests after the code?

Emergent design is a fundamental principal in Agile development, where the overall software architecture emerges through doing the simplest design to meet the currently implemented features and modifying the design as new features are added.

For the design to emerge as both clean and elegant, we will need to apply lots of small refactorings as we go. To know that we have applied a small refactoring correctly we need to know that we have preserved behaviour and not broken anything.

The simplest way to do this is to run the tests.
If you don't have the tests until you have written the code, you can't run the tests.
That's why, for each little bit of production code,
you need to write a test first!