Adding Tests First
Explore the significance of test-first methodology as a design tool in TDD, highlighting its role in uncovering design opportunities, forming executable specifications, and aiding continuous delivery.
In this lesson, we’ll review the trade-offs of adding a test first before writing the production code to make it pass. Previous chapters have followed a test-first approach to writing code. We write a test before writing production code to make that test pass. This is a recommended approach, but it’s important to understand some of the difficulties associated with it as well as consider its benefits.
Test-first is a design tool
The most important benefit of writing tests first is that a test acts as a design aid. As we decide what to write in our test, we’re designing the interface to our code. Each of the test stages helps us consider an aspect of software design, as shown by the following illustration:
The Arrange step helps us think about how the code under test relates to the bigger picture of the whole code base. This step helps us design how the code will fit into the whole code base. It gives us an opportunity to make the following design decisions:
What configuration data will be needed?
What connections to other objects or functions will be needed?
What behavior should this code provide?
What extra inputs are needed to provide that behavior?
Coding the Act step allows us to think about how easy our code will be to use. We reflect on what we’d like the method signature of the code we are designing to be. Ideally, it should be simple and unambiguous.
Some general recommendations are as follows:
The method name should describe the outcome of calling the method.
Pass in as few parameters as possible. Possibly ...