Test-Driven-Development
Test-Driven-Development is a software development technique in which the developer first writes a test for what they are planning the implement. Only after writing the first test is the implementation started.
1 Step-By-Step of the TDD-workflow
- Ensure all tests are passing.
- Write a test for the feature/bugfix/…
- Execute the tests, ensure the new test fails.
- If it fails, continue with (4)
- If it succeeds, go to (2)
- Write a minimal implementation which makes all tests pass
- Refactor the implementation
- Go to (1)
2 Mnemonic
To remember the 6 steps, I have a mnemonic: “Red, Green, Refactor”, which hints at the three overarching parts of the workflow: first writing a failing test, making the test pass and finally refactoring the implementation.
3 Benefits of Test-Driven-Development
3.1 No unnecessary code
By requiring a test to fail before touching the implementation, a feature has to actually be missing for it to get any implementation code. If the test doesn’t fail, the feature is somehow already supported and doesn’t require any extra code. Without writing the test first, the developer might write logic for this feature, although it is entirely unnecessary.
3.2 High, valuable test coverage
Every line of written implementation code has a corresponding test, since the implementation is only written once there is a test for it. This means that the tests do actually test the implementation instead of just executing it (Traditional Test Coverage is not valuable information).
3.3 The problem/requirements needs to be understood first
To be able to write a test before the implementation requires an understanding of what the code is supposed to do. This is impossible without understanding the problem or requirements leading to the code being required. Thereby the developer will need to think about the code they are writing not only on a logical, but also on a business (in the sense of business logic) level.
This means the developer understands why the code needs to be writting, which in turn can increase developer motivation and therefore productivity.