Refactoring test inputs with Test Data Builders

Test data builders is a pattern you can use to easily arrange test data, while improving their readability and reducing coupling between tests and source code.

Learning Goals

Session outline

Connect

Pick one of the following connect activities, depending on the trainee’s knowledge:

Map code examples with pattern names

Preparation

Activity

Concepts

Presentation: 4 rules of a Test Data Builder (3 min)

Explain the 4 rules of test data builders:

  1. Has an instance variable for each constructor parameter
  2. Initialises its instance variables to commonly used or safe values
  3. Has a build method that creates a new object using the values in its instance variables
  4. Has chainable public methods for overriding the values in its instance variables (aka with…() methods).

Demo (10 min)

Demo the refactoring of test inputs using a test data builder on an existing test.

Tips

Concrete Practice

Ask attendees to:

Conclusion

Does our code have this issue?

Ask the team to find part of their tests where they could have applied the test data builder pattern