16 Outline patterns that support higher-level steps
The chapter focuses on how to keep BDD scenarios concise and intention-revealing while still automating the hard parts behind the scenes. It explains that higher-level steps can hide mechanical details, but the supporting automation still has to manage resources, state, prerequisites, and complex outcomes in a way that remains understandable and maintainable.
For setup and dependency management, the text presents a set of patterns that reduce unnecessary overhead and improve flexibility. Test resources such as application hosts or payment gateways can be started and stopped with hooks, selectively enabled with tags, or handled through dependency injection when the resource is simple. When initialization order matters, hook ordering can make startup deterministic, and when a resource is needed only for some scenarios, tag expressions or scenario information can be used to control execution. The chapter also shows that some scenario resources are not technical at all, such as an authenticated user, and that the same lifecycle ideas apply there too.
For complex workflows and assertions, the chapter introduces shortcuts and structured output handling. A context shortcut can replace a long chain of setup actions with a faster test-only path, especially for Given steps that only need to prepare state, while the Ensure pattern lets shared context create missing prerequisites only when needed so steps can be reused across scenarios. For difficult outcomes like reports or other structured results, the Approved Outcome pattern uses a printer to convert the result into readable text and compares it with an approved version, giving clearer failures and atomic checking of all important details. Together, these patterns support faster, clearer, and more robust BDD automation.
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.
Effective Behavior-Driven Development ebook for free