The importance of TDD (Test-Driven Development)
Test-driven development is a methodology for software development that relies on the repetition of a short development cycle.
Read more about TDD in our previous blog here.
This short life-cycle is as follows:
- Write the test
- Run the test (there is no implementation code, test does not pass)
- Write just enough implementation code to make the test pass
- Run all tests (tests pass)
This cycle is often called also Red-Green-Refactor.
The idea is not to make the implementation final, but to provide just enough code for tests to pass. Once everything is green we can proceed to refactor the existing code. That means that we are making the code more optimum without introducing new features. While refactoring is in place, all tests should be passing all the time. If one of them fails, refactor broke an existing functionality. Refactoring should not include new tests.
Test-driven development is about development (and design), specifically improving the quality and design of code rather then about testing. The resulting unit tests are just an extremely useful by-product.
The T in TDD often confuses but what we really need this approach for is to force us to thinking about the implementation before writing the code.
Requirements of TDD are that our units can be easily mocked or stubbed. This forces us to have a clean design and be independent from external tools. For example in our unit tests we don’t need to connect to the database but mock it.
Another really nice feature that comes free with TDD is documentation. Sometimes by just reading the tests, one should understand what an implementation is for.
TDD is an awesome tool that we have that takes time to master but once understood productivity and quality increase drastically.
JIT Senior Software Engineer