Overview

14 Prop cupboard patterns for managing input test data

```html

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
Using domain defaults to create a customizable default instance
An object mother that supports a fluent interface
An object mother that supports a fluent interface
Data Table Accessor that supports optional columns
Data Table Accessor that supports optional columns
Test execution result on a failing comparison
Test execution result on a failing comparison
Implementing Validation Tester for multi-field validation
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.

FAQ

What is the main purpose of the Prop Cupboard patterns in this chapter?The Prop Cupboard patterns help manage input test data for BDD automation. They keep default values, default objects, object hierarchies, and domain-specific terminology in organized, reusable places so scenarios stay focused on behavior rather than incidental setup details.
What problem does the Domain Default pattern solve?It solves the problem of scattered hard-coded default values in automation code. By centralizing default test data in one place, it improves consistency, makes changes easier, and documents the team’s agreed defaults.
When should you introduce a DomainDefaults class?You should introduce it as soon as the first default value is needed. The chapter recommends centralizing defaults early rather than letting inconsistent values spread across the codebase.
What is the difference between the Domain Default pattern and the Default Instance pattern?Domain Default centralizes individual default values, like a pizza size or name. Default Instance builds a complete default object or object hierarchy using those defaults, often through a factory method that can be customized by the caller.
Why is the Default Instance pattern useful for mutable objects?Because the factory method returns a default object that the caller can then adjust for the scenario’s needs. This avoids duplicating object construction logic while still allowing custom overrides.
What is the Object Mother pattern used for?Object Mother is used to create more complicated default objects or hierarchies, especially when simple factory methods are no longer enough. It can accept customization instructions and build the final object structure for the test.
Why are fluent interfaces often used with Object Mothers?Fluent interfaces let configuration methods be chained together in a readable, sentence-like style. This reduces verbosity and makes the test code easier to follow, though it can add some implementation complexity.
What does the Terminology Promotion pattern achieve?It promotes domain-specific language in step definitions by using custom parameter types. This lets scenarios use business terms naturally while the automation code handles the conversion behind the scenes.
How does the Data Table Accessor pattern improve data table handling?It separates data table conversion from step logic and supports optional columns. This makes step definitions cleaner, more resilient to table changes, and easier to maintain.
What is the benefit of the Data Table Assertion pattern?It lets you compare expected and actual data tables in a single atomic assertion and provides detailed diff-style diagnostics when the comparison fails. This keeps the step definition simple while making test failures easier to understand.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Effective Behavior-Driven Development Effective Behavior-Driven Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Effective Behavior-Driven Development Effective Behavior-Driven Development ebook for free