Concept: Test-first Design
This concept describes a technique to bring test design chronologically in-line with software design.
Relationships
Main Description

Test-first design is the practice of writing developer tests and implementation code concurrently and at a very fine level of granularity.

In test-first design, the developer first writes a small portion of a developer test, and then writes just enough implementation code to make that developer test compile and execute. Then they write a little bit more of the test and then add enough code to make that new bit compile and pass. This cycle can last somewhere between 30 seconds and five minutes. Rarely does it grow to ten minutes. In each cycle, the tests come first. Once a unit test is done, the developer goes on to the next test until there are no more tests to be written for the implementation of the work item currently under development.

The practice of test-first design changes how the developer thinks. Tests are not written as an afterthought. Instead, developer tests are written as part of the everyday, every minute way of building software.

What are the advantages of test-first design?

  1. Assumptions in the design are analyzed before the implementation code is written. To write developer tests, an examination must be made of the behavior of each piece of code to be written, correct and incorrect behaviors must be defined. In a way, writing the tests before the code can be considered a version of detailed design.
  2. Code units designed for testability up front are cleaner and more loosely coupled.
  3. Errors are found earlier. Errors or gaps in the requirements and design are identified before coding begins when it could be more tempting to move ahead based on assumptions.
  4. A clearer collaboration strategy between the developer and others that might be responsible for the requirements, architecture, and design is put in place. During the creation of the tests, there must be a meeting of the minds as to what has been specified. After that, the implementation can carry on with confidence that there is a shared vision of what the code should do.
  5. There are unambiguous criteria for completion of the code. When the tests pass, the code is working as specified. Non-functional quality dimensions can be dealt with separately, but there is a clear moment when the code behaves correctly.
  6. The technique drives the developer to work in smaller increments with faster quality feedback. At any time the developer is just one test away from having error-free code.
  7. There is a separation of concerns and effort between getting code working and improving the quality of the code that already runs correctly. Separating out these two areas of concern provides focus and time management support to a developer who in one pass over the implementation makes it pass the tests as simply as possible and then in a subsequent pass, looks for areas to improve.