I have been using FeatureContext for storing information about a specific feature that can be reused in all scenarios in that feature. However I have some things like login information that I don't want to recreate for every feature. I want to use the [BeforeTestRun] attribute to store this, but does SpecFlow have some sort of Global Context feature (like log4net) to store this information so it can be reused by all features?
SpecFlow doesnt have a global context construct, however you have a few options that can help you share data between bindings/features:
Static members
You can use static field/property to set objects that can be used across all features during test execution. Keep thread safety in mind however, because if you run your tests in parallel you need to synchronize initialisation and any mutable access to this field. If you want this data to change per scenario you can set the object in the context when accessed, this way you can set a default, and allow the scenario to have its own copy.
Context injection
You can use SpecFlow Context injection to inject a object into step definitions via the constructor, this way you can initialize your type using its default constructor and the pass the type into the step def like so:
[Binding]
public class MyStepDefinition
{
private readonly MyContextData contextData;
public MyStepDefinition(MyContextData contextData)
{
this.contextData = contextData;
}
}
more info:
https://github.com/techtalk/SpecFlow/wiki/Sharing-Data-between-Bindings