What is Test-Driven Development?
Test-Driven Development (TDD) is a methodology in software development that focuses on an iterative development cycle where the emphasis is placed on writing test cases before the actual feature or function is written. TDD utilizes repetition of a short development cycle. It combines building and testing. This process not only helps ensure correctness of the code—but also helps to indirectly evolve the design and architecture of the project at hand.
TDD usually follows the "Red-Green-Refactor" cycle:
- Add a test to the test suite
- Run all the tests to ensure the new test fails (red)
- Write just enough code to get that single test to pass (green)
- Run all tests
This process sounds slower, and it often can be in the short-term, but it does improve the quality of the software project. Having adequate test coverage acts as a safeguard so you don't accidentally change the functionality. It's much better to catch a bug locally from your test suite than by a customer in production.
Finally, test suites can encapsulate the expectations of your software project so that your project's stakeholders (peers, future self) can better understand the project.
What to learn and practice Test-Driven Development? Check out the TestDriven courses.