Unit tests are never supposed to “replace” any other form of testing, but to be used together the whole time. Unit testing is a continuous learning process. As it is performed by developers, it is important for them to know how to unit test, what to unit test and its best practices.
TDD uses a “test first” approach in which test code is written before the actual code. These test cases are written one-at-a-time and followed immediately by the generation of code required to get the test case to pass. Software development becomes a series of very short iterations in which test cases drive the creation of software and ultimately the design of the program. Following are the steps of TDD:
- Write a “single” unit test describing an aspect of the program
- Run the test, which should fail because the program lacks that feature
- Write “just enough” code to make the test pass
- “Refactor” the code to the simplest level
- Repeat, “accumulating” unit tests over time