14 Prop cupboard patterns for managing input test data
This chapter introduces a set of patterns for keeping input test data under control so that scenarios can stay focused on behavior rather than on incidental setup details. The central idea is to treat test data like “props” stored in a prop cupboard: the scenarios use only what is essential, while the automation code takes responsibility for supplying the rest. Across the chapter, the authors emphasize that defaults should be agreed by the team, documented in one place, and reused consistently so that maintenance is easier and surprises are reduced.
The discussion begins with simple defaults and gradually moves toward richer object construction. A centralized place for domain defaults prevents hard-coded values from spreading through step definitions, and a default instance pattern makes it easy to create a prefilled object that can then be customized for a particular scenario. When the object becomes more complex, an object mother takes over the responsibility of assembling and tailoring whole object structures, often with a fluent style that reads naturally. The same thinking is extended to domain-specific terminology, where custom parameter types allow scenarios to express business language directly without pushing conversion clutter into step definitions.
The chapter then applies the same separation-of-concerns mindset to data tables and validation scenarios. A data table accessor pattern moves conversion and optional-column handling out of step logic, making tables more robust and easier to evolve. For assertions, a data table comparison approach replaces repetitive row-by-row checks with a single atomic comparison that still produces useful diff-style diagnostics when something fails. Finally, the validation tester pattern shows how default instances, scenario outlines, and targeted overwrites can describe complicated validation rules clearly, especially when only certain fields change from a known valid baseline. Together, these patterns help keep BDD code readable, maintainable, and aligned with the essential principle.
```Using domain defaults to create a customizable default instance
An object mother that supports a fluent interface
Data Table Accessor that supports optional columns
Test execution result on a failing comparison
Implementing Validation Tester for multi-field validation
Summary
- The Domain Default pattern provides a way to define default test data in a shared location to provide consistency across the test suite and avoid the maintenance issues of having hard-coded constants spread throughout the codebase.
- The Default Instance pattern shows how to avoid code duplication when building simple default objects and hierarchies using factory methods.
- Scenarios can modify a default instance if required, provided the factory method returns a mutable instance.
- When returning a mutable instance is not possible use the Object Mother pattern to provide customization instructions before constructing the instance to be returned.
- Fluent interfaces offer a clear, readable mechanism for chaining together customization instructions.
- Although enums can sometimes be used to directly model simple domain terminology, the more powerful technique described in the Terminology Promotion pattern uses custom parameter types to provide a more flexible modeling mechanism.
- Data tables are a concise and clear way to include complicated data in a step. Interacting programmatically with tabular representations is hard to read and maintain, so apply the Data Table Accessor pattern to parse the content of data tables into a data structure created expressly to model that data.
- It is not always essential to describe every available property of a data structure in every step. Ensure that the code parsing a data table, and the data structure that models the data, can handle optional properties and provide appropriate default values.
- Data tables can also be useful to express complicated expectations of a scenario. Comparing the actual and expected outcomes, when expressed as tables, is a powerful way support atomic assertions, but use the Data Table Assertion pattern to ensure complete diagnostics are provided across the full comparison.
- Testing data validation can get exponentially more complicated as the number of interacting attributes increase. Apply the Validation Tester pattern to spread data validation testing across multiple scenarios, each of which targets a specific rule that governs a relationship between a subset of the available attributes.
Effective Behavior-Driven Development ebook for free