liveBook · Manning

Context shortcut pattern

As explained in Effective Behavior-Driven Development

A testing pattern that establishes a scenario's required context directly, without repeating business workflows that are not under test.

Suppose an address-change scenario needs an order waiting for pickup. Replaying order placement and the kitchen workflow would establish that state, but neither workflow is what the scenario is trying to verify. A context shortcut prepares the required state directly, leaving the scenario focused on whether the address change is allowed. section 16.2

Use the shortcut behind a business-readable Given step, such as “the customer has an order that is waiting for pickup.” One implementation calls a test-only REST endpoint to prepare the order, then saves its order number for later steps. Use shortcuts only for Given steps: the actions and outcomes being tested must still run and be checked. section 16.2

Choose the shortcut with its trade-off in view:

  • It can make setup faster and less dependent on other workflows, allowing address-change behavior to be checked in isolation. section 16.2
  • It can also create an unrealistic order and produce a false result. Compare the prepared state with one reached through normal actions, and use programmer tests where needed to check the preparation code. section 16.2
Table 16.4 Comparison of application state ensuring strategies
Sequence of test actions
Context shortcut
Slower, more brittle
Faster, more robust
More dependent: The functionality that brings the application to the required state must already be implemented and working correctly to be able to verify the tested functionality
Loosely coupled: The functionality skipped by the shortcut can be independently scheduled and verified
Any bug in any of the test actions makes it impossible to verify the functionality illustrated by the scenario
The quality of the functionality illustrated by the scenario can be assessed in isolation
The verification is consistent with real usage
The verification might provide false results if the shortcut is not consistent with the result of the action sequence
The existing test drivers can be reused
The shortcut is an additional implementation task, and a dedicated backdoor API might be required that is only accessible from the tests

Compare replaying test actions with preparing state directly, including the speed, independence, and realism trade-offs. section 16.2

How Effective Behavior-Driven Development covers it

How deep it goes
Explains the Given-only boundary, a test-only endpoint implementation, and the risk of preparing unrealistic state.
Examples
Uses an order waiting for pickup before an address-change attempt, contrasting workflow replay with direct preparation.
What it assumes
Assumes the skipped ordering and kitchen workflows have already been verified; introduces direct state preparation as an alternative to replaying them.
What you can do after
Build it from scratch by preparing required state behind a Given step without bypassing the behavior being tested.
How it connects
Connects Given steps, scenario isolation, REST APIs, and programmer tests.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.