Test-Driven Development

Steven J Zeil

Last modified: Mar 14, 2019
Contents:

Abstract

Test-Driven Development treats unit testing as an integral part of the design and implementation process. Often summarized as “test first, code after”, TDD is actually a recognition that in writing tests, we are

1 Test-First Development

With our new knowledge of unit-testing frameworks, ideally, we have made it easier to write self-checking unit tests than to write the actual code to be tested.

1.1 Debugging: How Can You Fix What You Can’t See?

The test-first philosophy is easiest to understand in a maintenance/debugging context.

1.2 Test-Writing as a Design Activity

Every few years, software designers rediscover the principle of writing tests before implementing code.

Agile and TDD (Test-Driven Development) are just the latest in this long chain.

1.2.1 Tests are Examples

“If it’s hard to write a test, it’s a signal that you have a design problem, not a testing problem. Loosely coupled, highly cohesive code is easy to test.” – Kent Beck

2 Test-Driven Development

Test-Driven Development (TDD) is the practice of repeatedly:

  1. Write an automated test case for a new desired behavior.

    • This case must, initially, fail.

  2. Write just enough new code to pass the test.

  3. Refactor the code to make it acceptable quality.

This ties in very nicely with some of our previous discussion of incremental development. In particular, compare to the way we break stories into tasks.

2.1 The Cycle of Unit Test Failures

 

Here you can see a plot of test cases on the vertical axis versus time (actually, commits to the version control system) on the horizontal axis during a project on which I practiced TDD.

Tests passed are shown in blue and failed tests are shown in red.

Notice the repeated pattern:

3 TDD during Incremental Development

My stereotypical division of a story into tasks is typically

  1. Create/modify the API to describe a new desired behavior.
  2. Write the unit tests.
  3. Implement the new behavior.
  4. Integrate and commit changes.

Compare this to the steps of TDD, above, and you can see that they are compatible.

3.1 Case Study: TDD of a Spreadsheet Story

Here are some short videos illustrating my application of task 1 and task 2 of a story for the Embeddable Spreadsheet project.