16 Outline patterns that support higher-level steps
The chapter introduces a set of patterns for keeping BDD scenarios concise and intention-revealing while still making them fully automatable. It frames the problem as one of balancing readability against the practical needs of test setup, execution, and verification, especially when scenarios depend on expensive infrastructure, complicated state preparation, or structured results that are hard to assert directly.
For scenario setup, it explains how hooks, tags, and scenario context can manage test resources with scenario lifetime, and how tag expressions can limit costly initialization to only the scenarios that need it. It then contrasts that with dependency injection, which can be a simpler option for straightforward resources, while noting the importance of initialization order when resources depend on one another. For more complex prerequisite handling, the chapter shows how a step can “ensure” a needed condition only if it has not already been created, letting multiple steps reuse the same helper without duplicating setup logic.
For difficult workflows and assertions, the chapter presents the idea of a context shortcut: instead of simulating every business action in a long setup sequence, a scenario can prepare the system in the needed state through a test-only backdoor, as long as the shortcut remains consistent with real behavior and is validated appropriately. It also shows how approval-testing ideas can improve readability for complex outcomes by printing structured results into a human-readable form and comparing them atomically against an approved expectation, producing clearer diffs and better feedback when something changes.
Implementing scenario lifetime using automation tool hooks
The order process, showing the states in which an address change is possible
Contrasting context creation with and without using a context shortcut
Implementing the ensure pattern by checking a context variable to see if an entity needs to be created
Summary
- Scenario isolation may require expensive initialization of test resources. Use the Scenario Resource pattern to provide resource management and maintain fast feedback.
- Scenario automation tool hooks provide a natural mechanism for test resource management. Tag expressions, Order attributes, and scenario context APIs give a fine grained control over hook execution.
- Tags offer a way to add context on a scenario-by-scenario basis, without adding extra steps to the scenario.
- If only simple resource management is required, this can be provided by your DI framework without the need to implement any hooks.
- The Context Shortcut pattern supports intention-revealing Given steps by encouraging the naming of complicated contexts which are often the result of some business workflow. This provides a place to optimize context setup by implementing an alternative to mechanically replicating the user workflow.
- There are risks to Context Shortcut optimizations that must be analyzed and mitigated.
- Some steps have a prerequisite for test data. Some scenarios may need the prerequisite test data to be specified explicitly, while for others default data is sufficient. The Ensure pattern provides a mechanism for a step definition to satisfy its prerequisites and be safely used in any scenario.
- It is cumbersome to specify complicated outcomes (such as a report or an image) using simple Then statements. The Approved Outcome pattern uses Approval Testing techniques to make complicated outcomes easy to express and errors easy to understand.
- Approval Testing is a technique that simplifies the assertions on structured results by comparing them to a previously “approved” result and reporting any differences. Libraries are available that perform the comparison atomically and report all differences detected.
FAQ
What is the main purpose of the Scenario Resource pattern?
The Scenario Resource pattern helps manage scenario-lifetime test resources efficiently. It uses hooks, tags, or dependency injection to initialize and dispose of resources without adding unnecessary verbosity to the scenario text.
When should hooks be used to manage a test resource?
Hooks are a good choice when a resource needs explicit control over when it starts and stops, especially when it must be initialized before a scenario and cleaned up after it finishes.
How do tags help avoid starting expensive resources for every scenario?
Tags let you restrict hook execution to only the scenarios that need a resource. By tagging only the relevant scenarios and using tag expressions in hook attributes, you avoid unnecessary initialization for unrelated scenarios.
What is a tag expression in BDD automation?
A tag expression is a predicate over scenario tags used to decide whether a hook should run. It can use tag names alone or combine them with operators such as and, or, and not.
When is dependency injection a suitable alternative to hooks for resource management?
Dependency injection works well for simpler resources that can be created in a context class constructor and cleaned up in Dispose. It is less suitable when you need fine-grained control over initialization order or selective execution by tag.
What is the Context Shortcut pattern used for?
The Context Shortcut pattern is used to bring the application into the needed state for a Given step without simulating every business action. It improves performance and stability by skipping unnecessary workflow details.
Why should context shortcuts only be used for Given steps?
Because a shortcut is meant to prepare preconditions, not to implement the behavior under test. Using it in Given steps keeps the scenario focused on the behavior being verified and avoids mixing setup shortcuts with the actual action being tested.
What risk comes with using a context shortcut?
The main risk is inconsistency: the shortcut might prepare state differently from the real workflow. If that happens, tests could pass even though production behavior would fail, so the shortcut should be validated carefully, often with programmer tests.
What does the Ensure pattern solve?
The Ensure pattern helps step definitions reuse implicit prerequisites safely. If a prerequisite already exists, the ensure method does nothing; otherwise, it creates it, allowing multiple steps to rely on the same helper.
How does the Approved Outcome pattern improve complex assertions?
It uses a printer to convert complex results into a readable text form and compares that against an approved expected result. This makes complicated expectations easier to express and provides clearer, more atomic failure reporting than many separate assertions.
Effective Behavior-Driven Development ebook for free