Test-Driven Development
Test-Driven Development (TDD) is a software development process that relies on putting requirements first in the implementation phase. The TDD process follows these steps:
-
Add a test - First, write a test for the new functionality that needs to be added. The test will fail at this point and is referencing empty interfaces and/or empty class methods.
-
Write the code - Next, write the code required to make the new test pass. The goal is to write just enough code to pass the test; no extra code should be written at this point.
-
Run tests - Run the tests again. The new test should now pass.
-
Refactor as needed - The code can now be cleaned up as needed. Existing code can be restructured to improve maintainability as long as all tests continue to pass.
The TDD cycle then repeats itself - new tests are written for new functionality, code is written to pass the test, and the code is refactored. The result is code that is thoroughly tested and maintains high quality over time.
The process begins by writing tests for the happy path scenarios, then progresses to handling edge cases and providing proper error handling.
Benefits:
- Drives application design by focusing on test cases first, leading to better interfaces
- Ensures all code is covered by tests, providing confidence for programmers and users
- Finds defects earlier and avoids debugging
- Leads to better modularization, flexibility, extensibility, and test coverage compared to other approaches
With TDD, you build code to satisfy your tests, which represent business requirements, rather than writing arbitrary tests for approximate ideas of requirements.