- Use test data builders to create test inputs
- Explain how to make test inputs more expressive and readable
- Connect:
- Notes with the name of some following patterns, which can be used to create test inputs:
- a class constructor
- a class constructor with default values
- a factory method
- object mother
- test data builder
- test data builder + object mother combination
- For each pattern, provide a note with an example
- Randomize the notes
- Notes with the name of some following patterns, which can be used to create test inputs:
- Concept
- Slides to explain the 4 rules of test data builders
- An IDE with the kata you chose opened (some code generation capabilities are welcome)
Map code examples with a pattern
- Ask attendees to map code examples of test inputs construction with the corresponding patterns. (3 min)
- Discuss pros and cons of each pattern usage in a unit test suite context. (4 min)
Make a presentation of the 4 rules of Test Data Builders
:
- Has an instance variable for each constructor parameter
- Initialises its instance variables to commonly used or safe values
- Has a
build
method that creates a new object using the values in its instance variables - Has "chainable" public methods for overriding the values in its instance variables.
Demo the refactoring of test inputs using a test data builder on an existing test.
- use only with...() method names in a first time
- rename methods to make the object build more expressive (like natural language)
- replace usage of overriding methods when the value set is not relevant for the test case
- You can use the test-data-builders-kata, choosing the language of your choice, refactoring the Country creation.
- Use the generate code from usage method to focus on writing the code as you need to use it, and keep the demo short and clear.
Ask attendees to:
- reproduce the demo on the same input
- apply the same technique to all objects created in the
arrange
part of the tests
If you had to explain the main idea of using test data builders to someone else, in 1 sentence, what would you say ?