Overview

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.

FAQ

What is the Scenario Resource pattern used for?The Scenario Resource pattern is used to manage scenario-lifetime test resources efficiently, so expensive resources can be started before they are needed and stopped when the scenario ends without adding unnecessary scenario text.
When should hooks be used for test resource management?Use hooks when a resource needs explicit control over initialization and disposal timing, especially when the resource should be started before a scenario and cleaned up afterward.
How do tags help optimize expensive resource initialization?Tags let you restrict hook execution to only the scenarios that need a particular resource, avoiding the cost of starting that resource for every scenario.
What are tag expressions in scenario hooks?Tag expressions are predicates written in hook attributes to determine whether a hook should run based on the scenario’s direct or inherited tags, using operators like and, or, and not.
When is dependency injection a good alternative to hooks for resource management?Dependency injection is a good alternative for simpler resources, because the context class constructor can initialize them and IDisposable can clean them up automatically when the scenario ends.
What is the Context Shortcut pattern?The Context Shortcut pattern is a way to bring the application into the expected state for a Given step by using a faster shortcut instead of replaying the full business workflow.
When is a context shortcut appropriate?A context shortcut is appropriate only for Given steps, when the step is just preparing state and the functionality used to prepare that state has already been verified elsewhere.
What is the Ensure pattern used for?The Ensure pattern lets step definitions or shared context methods create an implicit prerequisite only if it does not already exist, which supports step reuse across scenarios with different setup needs.
Why use a printer in the Approved Outcome pattern?A printer converts complex structured results into a readable text form so the scenario can compare the actual output with an approved expected outcome in a clear, atomic way.
What problem does the Approved Outcome pattern solve?It solves the challenge of expressing and checking complicated expectations, such as reports or structured outputs, by combining approval testing ideas with BDD in a readable and maintainable format.

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


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 ebook for free
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 ebook for free