This week I was reading a book called Test Driven Development by Viktor Farcic. The first chapter is about why we need TDD.
The book list a few problems that all software developers face. I have listed a few from the book:
- Most, if not all, of your tests are manual, or you don’t have tests at all.
- Automated tests are written and executed when it’s too late for them to provide any real value for the project.
- The maintenance cost is too high.
- Documentation is never up to date.
- Team is spending too much time trying to figure out what some method or class does.
TDD does not magically solve any of these problems, but it sets us in the path toward the solution. TDD speeds up the time-to-market, enables easier refactoring, helps to create better design, and fosters looser-coupling.
The book goes on to explain something called the Red-Green-Refactor process. The procedure is simple:
- Write a test
- Run all tests
- Write the implementation code
- Run all tests
- Refactor
- Run all tests
Speed in key in this process; never spend more than a minute on writing tests or implementation. It’s like a game of ping pong, the game is very fast. Write a short test and run all tests (ping), write the implementation and run all tests (pong). Repeat until it’s time to refactor and confirm all tests are passing (score). It is important to understand that TDD is not about testing, it is a process to develop high quality software fast.
The two really interesting side effects of TDD are Executable documentation and No debugging. In TDD the tests serve as the documentation. If wan’t to find out what a particular method does, read the tests associated with it. This solves the problem of out of date documentation.
The second side effect is that we almost never need to debug applications we’re working on. We almost never debug because there is rarely a reason to debug. When the tests are written before the code we can have high confidence that the code is working as expected. And since we build the software in very small steps, any errors that might occur is very easily resolved.
I went on to read and follow chapter 3 and it really helped me improve my understanding of TDD.
From the blog Software Testing – The blog about software by Sudarshan and used with permission of the author. All other rights reserved by the author.