SpecFlow provides access to the current test context using both FeatureContext and the more commonly used ScenarioContext. FeatureContext persists for the duration of the execution of an entire feature, whereas ScenarioContext only persists for the duration of a scenario.

Accessing the FeatureContext

in Bindings

To access the FeatureContext you have to get it via Context-Injection.


public class Binding
	private FeatureContext _featureContext;

    public Binding(FeatureContext featureContext)
		_featureContext = featureContext;

Now you can access the FeatureContext in all your Bindings with the _featureContext field.

in Hooks


Accessing the FeatureContext is not possible, as no Feature is executed, when the hook is called.


You can get the FeatureContext via parameter of the static method.


    public class Hooks
        public static void BeforeFeature(FeatureContext featureContext)
            Console.WriteLine("Starting " + featureContext.FeatureInfo.Title);

        public static void AfterFeature(FeatureContext featureContext)
            Console.WriteLine("Finished " + featureContext.FeatureInfo.Title);


Accessing the FeatureContext is done like in normal bindings


Accessing the FeatureContext is done like in normal bindings

Storing data in the FeatureContext

FeatureContext implements Dictionary<string, object>. So you can use the FeatureContext like a property bag.


FeatureInfo provides more information than ScenarioInfo, but it works the same:

In the .feature file:

    Scenario: Showing information of the feature
  When I execute any scenario in the feature
  Then the FeatureInfo contains the following information
	| Field          | Value                               |
	| Tags           | showUpInScenarioInfo, andThisToo    |
	| Title          | FeatureContext features             |
	| TargetLanguage | CSharp                              |
	| Language       | en-US                               |
	| Description    | In order to                         |

and in the step definition:

    private class FeatureInformation
        public string Title { get; set; }
        public GenerationTargetLanguage TargetLanguage { get; set; }
        public string Description { get; set; }
        public string Language { get; set; }
        public string[] Tags { get; set; }

    [When(@"I execute any scenario in the feature")]
    public void ExecuteAnyScenario() { }

    [Then(@"the FeatureInfo contains the following information")]
    public void FeatureInfoContainsInterestingInformation(Table table)
        // Create our small DTO for the info from the step
        var fromStep =  table.CreateInstance<FeatureInformation>();
        fromStep.Tags = table.Rows[0]["Value"].Split(',');

        var fi = _featureContext.FeatureInfo;
        // Assertions
        for (var i = 0; i < fi.Tags.Length - 1; i++)

FeatureContext exposes a Binding Culture property that simply points to the culture the feature is written in (en-US in our example).