Testing First Drives Design
Learn about test-driven development and its process through a diagram.
Test-driven development (TDD)
TDD is the counterintuitive idea that developers will improve their code’s design and accuracy by writing the tests before writing the code. When adding new logic to a program, the TDD process starts by writing an automated test that describes code behavior that does not yet exist. A new logic is added to the program in a strict TDD process only in response to a failing test.
Code design guidance
Writing tests before code rather than after allows our tests to help guide our code’s design in small, incremental steps. Over time this creates a well-factored codebase that’s easy to change.
Test-driven development (TDD) process
Success with test-driven development starts with trusting the process. The classic process goes like this:
-
Create a test. The test should be short and test for one thing in our code. The test should run automatically.
-
Make sure that test fails. Verifying the test failure before writing code helps ensure that the test does what we expect.
-
Write the most straightforward code that could make the test pass. Don’t worry about ...