Outline Step Bindings That Outlive Scenarios
Scenario Outlines are one of the most-used features in Cucumber-JVM 7, Behave, and SpecFlow — and one of the least-understood at the binding layer. The common mental model is that each row in an Examples table produces an independent scenario, with its own setup, its own step execution, and a clean slate afterward. That model is correct for scenario data. It is not always correct for the step definition objects that service those scenarios.
The real problem surfaces when a step binding holds mutable state — a shared client, a cached token, a Playwright browser context — and that state is not reset between outline rows. Row two silently inherits row one's side effects. The failure, when it comes, looks like a flaky test rather than a design defect, which is why it survives code review and lands in CI.
By the end of this article you will understand the object lifecycle that governs step bindings across outline rows in the major BDD frameworks, know the specific patterns that cause state to leak, and have concrete remediation code for Cucumber-JVM, Behave, and SpecFlow.
Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.
Step vs. Steps: What the Binding Lifecycle Actually Governs
In Cucumber-JVM 7, a step definition is a method annotated with @Given, @When, or @Then. The class that contains it — the glue class — is instantiated once per scenario by default when you use PicoContainer or Spring injection. A Scenario Outline with five rows is five scenarios, so PicoContainer constructs five fresh glue instances. That is the happy path. The failure path is when engineers reach for a static field, a singleton service, or a Spring @Scope("singleton") bean to share state across steps within a single scenario — and that shared object then bleeds across rows because its scope exceeds the scenario boundary.
The distinction between step (a single annotated method) and steps (the full glue class that may contain dozens of methods and shared fields) is where most teams go wrong. A regex like @Given("^the user {string} is logged in$") looks stateless. The WebDriver field sitting two lines above it in the same class is not. Understanding how outline tables multiply brittle step bindings is prerequisite knowledge here — the binding count problem and the lifecycle problem compound each other.
Diagnosing and Fixing Binding State Across Outline Rows
Start with a minimal reproduction. The following Scenario Outline in Cucumber-JVM is enough to expose the problem:
Feature: Cart pricing
Scenario Outline: Apply discount codes
Given the cart contains <item> at £<price>
When discount code <code> is applied
Then the total should be £<expected>
Examples:
| item | price | code | expected |
| Widget | 20.00 | SAVE10 | 18.00 |
| Gadget | 50.00 | SAVE20 | 40.00 |
| Doohick | 15.00 | INVALID | 15.00 |
Now look at the glue class. The dangerous pattern is a CartService field that is initialised in a @Before hook but never cleared:
// Cucumber-JVM 7 — problematic glue class
public class CartSteps {
private CartService cart; // shared across ALL rows if singleton
private double runningTotal; // mutable — leaks between rows
@Before
public void setUp() {
// PicoContainer creates a new CartSteps per scenario — safe here.
// But if CartService is Spring singleton, it is NOT recreated.
cart = CartServiceFactory.getInstance(); // static factory = same object
}
@Given("the cart contains {string} at £{double}")
public void addItem(String item, double price) {
cart.addItem(item, price);
runningTotal = price;
}
}
CartServiceFactory.getInstance() returns the same object for every row. Row two's addItem call appends to whatever row one left in the cart. The fix is scope enforcement, not discipline:
// Cucumber-JVM 7 — safe pattern with PicoContainer
public class CartSteps {
private final CartService cart;
public CartSteps(CartService cart) { // injected — PicoContainer creates
this.cart = cart; // a new CartService per scenario
}
}
# picocontainer dependency in pom.xml
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>7.15.0</version>
<scope>test</scope>
</dependency>
In Behave, the equivalent risk is state stored on context without explicit cleanup. Behave reuses the same context object across all rows of an outline, resetting only the attributes set in before_scenario. Any attribute written during a step that is not explicitly cleared will survive into the next row:
# environment.py — Behave
def before_scenario(context, scenario):
context.cart = CartService() # fresh per row — correct
context.discount_result = None # explicit reset — correct
def after_scenario(context, scenario):
context.cart.teardown() # release connections
In SpecFlow with .NET 6+, the equivalent is constructor injection scoped to [Binding] classes. SpecFlow's built-in DI creates a new instance per scenario by default; the trap is registering a shared service as a singleton in ScenarioContext or via a custom IObjectContainer override. Teams that migrate from SpecFlow 2 to SpecFlow 3+ often carry over manual ScenarioContext.Current patterns that bypass the DI scope entirely. After fixing scope enforcement in one platform team's SpecFlow suite, outline-related flakiness dropped from roughly 14% of nightly runs to under 2% — without changing a single scenario.
Where Senior Engineers Still Get Burned by Outline Binding State
The most common mistake is treating @Before and @After hooks as a substitute for proper dependency scope. Hooks fire per scenario (per outline row), which looks correct — but if the object being "reset" in @Before is the same instance every time (because it was obtained from a static registry or a framework singleton), the reset is cosmetic. This is an org-level problem as much as a tooling one: hooks are visible in the test file, so they feel like sufficient cleanup. The actual scope of the injected dependency is buried in a config file that fewer people read. The same pattern causes step definitions to outlive the features they were written for — the binding persists long after its context has changed.
A second failure mode is Playwright browser context reuse. Teams share a BrowserContext across outline rows to save the ~300 ms launch overhead per row. The saving is real; the cost is that cookies, localStorage, and in-flight network intercepts from row one are present in row two. Use browser.newContext() per scenario and accept the overhead, or profile carefully with Playwright's built-in tracing before deciding context reuse is worth the risk. If you do reuse, step retry logic will mask the resulting flakiness rather than surface it.
Myths About Outline Isolation That Persist in Production Suites
Myth 1: Each outline row is a fully isolated test. It is isolated at the Gherkin data level. It is not automatically isolated at the infrastructure level. The JVM process, the browser binary, the database connection pool, and any static application state all persist across rows unless you explicitly scope and reset them. Frameworks give you the tools; they do not enforce isolation by default. This is also why Background blocks can outlive the scenarios they serve — the same scoping blind spot applies.
Myth 2: The Java Cucumber step regex is the unit of reuse. The regex (@Given("^the user {string} is logged in$")) is the matching unit. The reuse unit is the glue class, and it carries state. Engineers optimise regex patterns for DRY step matching while ignoring that the class holding those methods accumulates fields that couple unrelated scenarios. The right question is not "does this regex match both scenarios?" but "does sharing this class instance between those scenarios introduce hidden coupling?" Keeping step definition count low only helps when the bindings that remain are genuinely stateless or properly scoped.
Outline step binding lifecycle is not a framework quirk — it is a design constraint that compounds at scale. Audit your glue classes for mutable fields, verify that your DI scope matches your scenario boundary, and add a post-outline state-dump assertion in staging if you suspect silent leakage. The next thing worth measuring after you fix scope is mean-time-to-detect on the flaky tests that disappear: if they stop appearing within two nightly runs, you found the root cause.
Note: This article is for informational purposes only and is not a substitute for professional advice. If you need guidance on specific situations described in this article, consider consulting a qualified professional.