DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Use ScenarioContext in SpecFlow (and What to Use for New Projects)

Inject ScenarioContext into SpecFlow bindings to share temporary data within one scenario. Learn the scope, parallel-execution pitfalls, and migration considerations for Reqnroll.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In an existing SpecFlow project, inject ScenarioContext into a binding class or scenario-level hook, then use that instance to share data between steps in the same scenario. Avoid ScenarioContext.Current in SpecFlow 3 or later. SpecFlow reached end of life on December 31, 2024; for new work, evaluate Reqnroll and check its current migration guidance and package compatibility.

What ScenarioContext is for

ScenarioContext represents the active scenario. It can hold temporary test data that one step writes and another step reads, including when those steps belong to separate binding classes. It is test-execution state, not durable application storage.

As an Amazon Associate I earn from qualifying purchases.

Use it for a value that is genuinely shared within one scenario. For long workflows, prefer focused scenarios and explicit setup so that a large collection of hidden, string-keyed dependencies does not make tests difficult to understand.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inject ScenarioContext into a binding

Constructor injection makes the dependency visible and gives the binding access to the context for its active scenario. This illustrative C# pattern stores a customer name in one step and reads it in another:

using TechTalk.SpecFlow;

[Binding]
public class AccountSteps
{
    private readonly ScenarioContext _scenarioContext;

    public AccountSteps(ScenarioContext scenarioContext)
    {
        _scenarioContext = scenarioContext;
    }

    [Given("I have a customer")]
    public void GivenIHaveACustomer()
    {
        _scenarioContext["customer"] = "Ada";
    }

    [Then("the customer is available")]
    public void ThenTheCustomerIsAvailable()
    {
        var customer = (string)_scenarioContext["customer"];
        // Assert or otherwise use customer here.
    }
}

Use descriptive, consistent keys, and write the value before any step attempts to read it. In a real test, add an assertion appropriate to the behavior being checked; the example only demonstrates storing and retrieving scenario data.

Use the context only where a scenario is active

Scenario- and step-level hooks run in an active scenario and can receive its context. Test-run and feature-level hooks run without an active scenario, so they cannot use that scenario’s ScenarioContext. Scenario metadata such as title and tags is available through ScenarioContext.ScenarioInfo.

If information must be shared across scenarios in the same feature, consider FeatureContext instead. Do not substitute static shared state without considering parallel execution: scenarios running concurrently can interfere with one another when they mutate shared data.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the right place for test state

Need Approach Scope and trade-off
Pass temporary data between steps in one scenario Injected ScenarioContext Scoped to the active scenario; useful across binding classes, but opaque keys can hide dependencies.
Share data across scenarios within a feature FeatureContext Feature-wide rather than scenario-wide; consider interactions and parallel execution.
Make a scenario’s prerequisites easy to understand Explicit step inputs and focused scenario setup Reduces hidden coupling and the accumulation of unrelated values in a context property bag.

Do not use ScenarioContext.Current in new code

ScenarioContext.Current was marked obsolete in SpecFlow 3.0 and later. Prefer injecting the context, especially in suites that may run scenarios in parallel. Constructor injection also makes a binding’s use of scenario state explicit instead of relying on legacy static access.

Account for SpecFlow’s end of life

Tricentis ended SpecFlow on December 31, 2024. Reqnroll is described as retaining the ScenarioContext and FeatureContext APIs, with the namespace changing from TechTalk.SpecFlow to Reqnroll. Treat that as a migration starting point, not a guarantee that an existing suite will work unchanged: consult current Reqnroll migration documentation and verify package compatibility for your project before switching.

Troubleshoot common problems

  • A read fails or returns no useful value: check that a prior step stored the value under the same key, with the expected type, before the read occurs.
  • A hook cannot obtain ScenarioContext: check its scope. Test-run and feature hooks do not have an active scenario context; use a scenario- or step-level hook for scenario state.
  • Tests interfere when run in parallel: remove shared static mutable state and use scenario-scoped injection for values that belong to one scenario.
  • A SpecFlow project fails after a framework or package upgrade: do not assume end-of-life SpecFlow code is compatible with current .NET or Reqnroll packages. Check the migration documentation and package compatibility, then resolve namespace and dependency changes deliberately.
  • A context has become hard to maintain: replace unrelated or implicit key-value dependencies with focused scenarios and explicit setup where practical.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your testing work also needs website captures, ScreenshotNeo is a separate website screenshot API and MCP server for developers—not a replacement for SpecFlow’s scenario state. Its API can return a screenshot or PDF from one GET request. The following cURL example saves a WebP capture; see the ScreenshotNeo API documentation for options and response details.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf for AI agents. The Free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Sign up for ScreenshotNeo’s free plan: 1,000 screenshots a month, no card required.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.