From Kent Beck’s book Test-Driven Development By Example I’ve learned a few quite simple rules.
The two essential rules:
- Write new code only if there is a failing automated test
- Eliminate Duplication
The more systematic way of doing TDD:
- Add a test
- Run all tests and fail
- Make a little change
- Run tests and pass
- Refactor to remove duplication
This method can be broken down even further to:
- Red – write a failing test
- Green – make it pass
- Refactor – remove duplication
This is more widely known as the TDD Mantra.
The ability to work in small steps thus strengthens our confidence in the software we build.
In programming, if you fear the unknown, write some tests!