- Introduce Test-Driven Development (TDD).
- Explain the benefits of TDD.
Test Driven Development (TDD) is the practice of writing unit tests prior to implementing the functionality that is meant to be tested. This may sound counter-intuitive, but it has the following benefits:
- Unit testing forces us to think about the structure of the code and making sure the code is written in a way that it can actually be tested. Going through that process prior to actually writing the code saves time and leads to better written software sooner.
- It forces us to have the mindset of "if we cannot test it, we cannot write the code." We cannot write code that isn't covered by unit tests.
The common steps to TDD are the following:
- Write a unit test to test a certain functionality.
- Run the test (which will fail the first time around since we have not written any code yet in the program).
- Write some code and/or refactor the code to make the test pass.
- Execute the test(s) again and see if it passes.
- If it does not, go back and refactor the code until it does pass.
- Repeat the following steps by adding unit tests any time we want to add additional features or functionality.
Below is a diagram illustrating the lifecycle of test driven development:
Note that we "stop" or end the cycle when we are finished developing the program.