liveBook · Manning

Local scenario context

As explained in Effective Behavior-Driven Development

A scenario-execution pattern that stores information in the local context so later steps can share it without repeating it.

An order-cancellation scenario can become hard to read if every step repeats the customer name and order number. Local Scenario Context lets a later step use information established earlier, so the scenario can say “the placed order” instead of repeating its number. section 13.1

The pattern stores information for one scenario execution and makes it available to later step definitions in the same class or file. Those steps can work together as a business narrative without each step being context-independent. section 13.1 section 13.0

Remember the placed order

After a step records that Rebecca placed order #12342, a cancellation step can refer to “the placed order.” Its step definition retrieves the saved number and cancels that order. The scenario introduces the number once while the automation retains what the later step needs. section 13.1

Listing 13.3 Using the step definition class fields for order number
*** StepDefinitions/OrderingStepDefinitions.cs ***
[Binding]
public class OrderingStepDefinitions
{
  private int placedOrderNo;

  [Given("the customer {string} has placed the order #{int}")]
  public void GivenTheCustomerHasPlacedTheOrder(string customerName, 
      int orderNo)
  {
    OrderService.PlaceOrder(customerName, orderNo, "Margherita");
    placedOrderNo = orderNo;                           #A
  }

  [When("the customer {word} cancels the placed order")]
  public void WhenTheCustomerCancelsThePlacedOrder(string customerName)
  {
    OrderService.CancelOrder(customerName, 
      placedOrderNo);                                  #B
  }
}

The step-definition class saves the order number in an instance field when the order is placed, then reads that field when the placed order is cancelled. section 13.1

Keep that memory local to the scenario. Automation tools create fresh glue-code instances for each execution, isolating instance fields from other scenarios. Avoid static fields: their values persist between executions, making results depend on earlier scenarios and preventing parallel execution. When steps live in different classes or files, this local pattern does not share their data; Shareable Scenario Context addresses that broader need. section 13.1 section 13.0

How Effective Behavior-Driven Development covers it

How deep it goes
Shows an instance-field implementation and its isolation rationale, while limiting the pattern to steps in one class or file.
Examples
Contrasts repeated order numbers with “the placed order” in an order-cancellation scenario and shows an instance field carrying that number.
What it assumes
Assumes familiarity with scenarios and step definitions, then introduces how their execution context can retain information.
What you can do after
Build it from scratch with per-scenario instance fields so later steps reuse details without leaking them into other executions.
How it connects
Connects step definitions and scenario execution context with Shareable Scenario Context and the Introduce and Refer pattern.

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.