Chapter 16: Outline patterns that support higher-level steps
The introduction explains the tension between keeping scenarios concise and automating the mechanical details needed for complex behavior.
After this chapter you will be able to
- High-Level Scenarios and Automation. Explain why scenarios should convey the intent of a behavior without spelling out inessential mechanics, while the automation code must still handle those details in an understandable, maintainable way.
- Scenario Resource Lifetime. Explain how a scenario-lifetime test resource is started before it is needed and disposed of when the scenario finishes, so scenarios remain isolated. Distinguish a resource needed by every relevant scenario, such as the application host, from one needed by only some, such as the payment gateway.
- Selective Resource Hooks. Describe how tags and hook tag expressions restrict resource initialization to scenarios that need it, including tags inherited from a Rule or Feature. Explain when filtering inside hook code is needed instead.
- Context Shortcut Boundary. Describe how a Context Shortcut prepares the state needed by a Given step without replaying a previously tested workflow, and distinguish this from testing the workflow itself.
- Shortcut State Consistency. Explain the risk that shortcut-prepared state differs from state produced by the normal action sequence, and describe the safeguards discussed here.
- Conditional prerequisite creation. Describe how the Ensure pattern handles a step that sometimes follows an explicit order-placement step and sometimes needs an implicit order.
- Complex Outcome Assertion Problem. Explain why separate Then-step assertions were a poor fit for the sales report and how an approved outcome addresses that problem.
- Approved Result Comparison. Describe how approval testing compares a structured result with a previously approved result and how the team treats differences.
- Printable Outcomes and Diffs. Explain why the sales report is converted to readable text and how a text diff helps assess its changed output.
What it builds on
- Document the essence of the behavior (chapter 7) · The aim of documenting the essence of behavior in scenarios.
- Automating though an outer interface (chapter 15) · The application host that must run for REST API scenarios.
- Hooks (chapter 12) · Before- and after-scenario hooks.
- Tags are documentation too (chapter 10) · Tags attached to scenarios and their enclosing documentation.
- Shareable Scenario Context pattern: sharing data between step definitions in different classes (chapter 13) · Shareable scenario context for step definitions.
- The principles (chapter 7) · The BRIEF principle of expressing intent rather than mechanics.
- Test Driver pattern: separating automation logic (chapter 15) · The Test Driver pattern for separating automation logic.
- Setting the context (chapter 9) · Setting the context in formulated scenarios
Section by section
Introduction
The introduction explains the tension between keeping scenarios concise and automating the mechanical details needed for complex behavior.
- High-Level Scenarios and Automation: Explain why scenarios should convey the intent of a behavior without spelling out inessential mechanics, while the automation code must still handle those details in an understandable, maintainable way.
Why it matters: It explains why readable scenarios and maintainable automation must be designed together.
Key terms: formulation, high-level scenarios, mechanical details, scenario automation, BRIEF scenarios
16.1 Scenario Resource pattern: managing test resources
This section explains how the Scenario Resource pattern manages per-scenario test resources, including selective startup, hook ordering, access from tests, and a dependency-injection alternative.
- Scenario Resource Lifetime: Explain how a scenario-lifetime test resource is started before it is needed and disposed of when the scenario finishes, so scenarios remain isolated. Distinguish a resource needed by every relevant scenario, such as the application host, from one needed by only some, such as the payment gateway.
Why it matters: Managing each resource for the scenarios that need it preserves isolation without unnecessary startup costs. - Selective Resource Hooks: Describe how tags and hook tag expressions restrict resource initialization to scenarios that need it, including tags inherited from a Rule or Feature. Explain when filtering inside hook code is needed instead.
Why it matters: Selective hooks avoid paying the startup cost of a resource in scenarios that never use it. - Deterministic Hook Initialization Order: Explain why dependent resources may require an enforced initialization order and how the hook Order property provides one. State why after-hook ordering should be checked against the particular automation tool.
Why it matters: Explicit ordering prevents an application from starting before a resource it depends on is ready. - Resource Access During Execution: Distinguish how a resource is supplied to the application from how step definitions gain access to it for setup or assertions.
Why it matters: Starting a resource is not sufficient if the application or test steps cannot reach it. - Dependency Injection Alternative: Describe how an injected context can initialize a simple resource when created and dispose of it at scenario end, and contrast this with the explicit timing and tag selection available through hooks.
Why it matters: The choice between hooks and injection affects when resources start and how clearly selective startup is controlled.
Key terms: Scenario Resource pattern, test resources, scenario-lifetime, application host, payment gateway, tags, hook tag expressions, direct or inherited tags, CombinedTags, Order property
16.2 Context Shortcut pattern: bring the application to the expected state
This section explains how Context Shortcuts speed up complex Given-step setup while preserving the reliability and boundaries of scenario tests.
- Intention Revealing Step Optimization: Explain why a single intention-revealing Given step can be optimized more easily than a sequence of separate action steps, and when that optimization becomes useful.
Why it matters: A clear scenario step leaves room to improve slow or unreliable setup without rewriting the specification. - Context Shortcut Boundary: Describe how a Context Shortcut prepares the state needed by a Given step without replaying a previously tested workflow, and distinguish this from testing the workflow itself.
Why it matters: The shortcut saves setup time without making the scenario skip the behavior it claims to test. - Shortcut State Consistency: Explain the risk that shortcut-prepared state differs from state produced by the normal action sequence, and describe the safeguards discussed here.
Why it matters: A fast passing scenario is not trustworthy if its prepared data could not represent a real order. - Backdoor Driver Separation: Describe how a test-only backdoor API and dedicated backdoor driver can implement a shortcut, and explain how access to that API should be controlled.
Why it matters: Clear separation prevents setup shortcuts from being confused with the application's real behavior and keeps test-only access controlled. - Pooled Prepared Entities: Describe the pooled-entity variation used when preparing test data remains slow.
Why it matters: Pre-prepared data can shorten the feedback loop even when creating each entity cannot be made fast.
Key terms: intention-revealing steps, Given steps, test actions, Context Shortcut pattern, Given step, When step, application state, programmer tests, inconsistent application state, data-handling infrastructure
16.3 Ensure pattern: managing implicit dependencies
This section explains how the Ensure pattern supplies implicit functional prerequisites during automation without cluttering scenarios or replacing prerequisites they specify explicitly.
- Implicit functional prerequisites: Explain why an intention-revealing scenario can omit an obvious functional prerequisite even though its automation must still provide that prerequisite.
Why it matters: It keeps the scenario focused on the intended behavior without overlooking what execution requires. - Conditional prerequisite creation: Describe how the Ensure pattern handles a step that sometimes follows an explicit order-placement step and sometimes needs an implicit order.
Why it matters: The same step can work with either a scenario-specific order or an implicit default order. - Shared ensure helper boundaries: Explain why multiple step definitions can call a shared ensure helper and when using the pattern becomes counterproductive.
Why it matters: A shared helper supports step reuse without hiding an expanding chain of unrelated setup.
Key terms: implicit context, functional prerequisites, Given step, Ensure pattern, EnsureOrderPlaced, OrderingContext, explicit Given step, default order, shareable scenario context, ensure method
16.4 Approved Outcome pattern: atomic handling of complex assertions
The section shows how the Approved Outcome pattern makes complex scenario expectations more readable by comparing a printable result with an approved one.
- Complex Outcome Assertion Problem: Explain why separate Then-step assertions were a poor fit for the sales report and how an approved outcome addresses that problem.
Why it matters: It explains why one readable expectation can be more useful than many narrow checks. - Approved Result Comparison: Describe how approval testing compares a structured result with a previously approved result and how the team treats differences.
Why it matters: It prevents the assistant from treating every changed output as automatically wrong—or automatically acceptable. - Printable Outcomes and Diffs: Explain why the sales report is converted to readable text and how a text diff helps assess its changed output.
Why it matters: A readable representation and useful diff make a complex report’s changes inspectable. - Readable Approved Result Placement: Describe the trade-off between keeping the approved report in the scenario and storing it in an external file.
Why it matters: Where the expected result lives affects whether readers can review the scenario without extra effort.
Key terms: Then steps, Approved Outcome pattern, atomic assert, approval testing, approved result, snapshot testing, printer, text diff, printed result, docstring
16.5 Summary
Where this is used later
- Test Log pattern: recording scenario execution details (chapter 19) · Access to information about the executing scenario, including its tags.
- (chapter intro) (chapter 21) · Managing the lifetime of resources needed by a scenario.
- Resource Pooling pattern: leasing pooled resources to scenarios running in parallel (chapter 21) · DI-based scenario resource usage and disposal
- In-place Persona pattern: persona definitions in feature files (chapter 17) · Creating the state needed by a scenario through a Context Shortcut.
- Corroboration Check pattern: monitoring the application’s health indicators (chapter 19) · context setup using the Context Shortcut pattern
- Templated Persona pattern: customizing external data sets (chapter 17) · The Ensure pattern cited for doing the building when the data is first needed
- External Persona pattern: load test data from external sources (chapter 17) · The Approved Outcome pattern referred to by the example's approved result.
Concepts explained in this chapter
- context shortcut patternA testing pattern that establishes a scenario's required context directly, without repeating business workflows that are not under test.
- scenario resource patternThe Scenario Resource pattern manages resources that must be initialized for an individual scenario while preserving independent execution and efficient feedback.
- test resourceA test resource is an infrastructure component or other resource whose lifetime and availability are managed for test execution.
- approved outcome patternThe Approved Outcome pattern represents a complicated expected result through an approved reference result that can be compared during testing.
- ensure patternA code-structuring pattern that lets a step definition create an implicit functional prerequisite only when no explicit prerequisite exists.
- snapshot comparisonApproval testing verifies structured results by comparing them with a previously approved result and reporting detected differences.
- printerA method or class that converts a result into a format that is easier to compare and approve.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
