Golden Rule of TDD

When people start with TDD I often see them struggle to remember to write the test code first. They may have that intention, but then forget all to easily. This session helps them practice this skill, and to remember why it’s important.

Learning Objectives

Session Outline

Connect - things to remember when doing TDD

Instead of the above connect, do this learning hour a second time as a refresher, with a different connect.

Put this question to the group, and ask them to come up with 5 answers.

“What are 5 important things to remember when you’re doing Test-Driven Development?”

The kinds of answers you’re looking for are:

The ‘concept’ part of this learning hour emphasizes the ‘write the test first’ guideline, but you can also write up and highlight the other guidelines they come up with.

Concept: Golden Rule

In TDD the tests drive the development of the production code. You can express that as a golden rule:

Do not write any production code until you have a failing test that requires it

Declare new classes, methods etc, in test code. Then use your tools to bring those things into existence.

Write the golden rule on a flipchart and make it look pretty. Ask people what they think of it.

You might mention there is an exception to the Golden Rule: you are allowed to declare new classes and methods while refactoring, so long as they are already covered by existing tests.

Do: Shopping Basket

Practice writing the tests first before creating the classes and functions they describe. The Shopping Basket Kata might be a good one.

Before you split into pairs to work on the kata, spend a few minutes in the whole group coming up with a test list. Something like:

When they are doing the kata, try to stop them from creating a ShoppingBasket class or Product class or calculatePrice function without first creating a test case. Have them practice using things in the test before they exist in the production code.

Reflect: Design in TDD

Does it help you design better classes and interfaces? Does using TDD prevent you from designing more than you need? Discuss in pairs.