Two Types in SpecFlow - ScenarioContext and FeatureContext

· March 26, 2011

Recently, I had the chance to explore more advanced error handling with SpecFlow and delved into the ScenarioContext and FeatureContext objects. These components offer some powerful capabilities that are worth understanding. In this post, I’ll take a closer look at both and offer some tips on how to utilize them effectively in your SpecFlow projects.

ScenarioContext

Most of us have encountered the ScenarioContext in the code generated by SpecFlow when a step definition is missing:

[Binding]
public class StepDefinitions
{
    [Then(@"I don't have this step definition in place yet")]
    public void ThenIDonTHaveThisStepDefintionInPlaceYet()
    {
        ScenarioContext.Current.Pending();
    }
}

However, ScenarioContext provides several other useful features:

ScenarioContext.Pending

The Pending method is well-known for marking steps as pending when a step definition isn’t implemented. You can also use it directly if needed. Here’s a demo scenario for this.

ScenarioContext.Current

ScenarioContext.Current offers a Dictionary for storing values between steps, making it easier to manage state within step definitions. Use type-safe extension methods from the TechTalk.SpecFlow.Assist namespace to handle these values safely.

Darren Cauthon has a video tutorial on this and further documentation on SpecFlow’s wiki. Here’s an example of its usage:

ScenarioContext.ScenarioInfo

This property provides access to information about the current scenario, such as its title and tags. It can also be used to handle errors by accessing ScenarioContext.Current.TestError, which holds the exception that occurred. Here’s a simple error handling example using this feature.

ScenarioContext.Current.CurrentScenarioBlock

This property reveals the current step type (Given, When, Then), which can be useful in some cases, although its immediate use may not be apparent.

FeatureContext

FeatureContext operates at the feature level rather than the scenario level. Here’s how it compares:

FeatureContext.Current

Similar to ScenarioContext.Current, this property also provides a Dictionary for storing values, but it persists throughout the entire feature execution. Check out the code for FeatureContext scenarios here.

FeatureContext.FeatureInfo

FeatureContext.FeatureInfo offers more detailed information about the feature, including its metadata and binding culture.

Conclusion – What’s It Good For?

Understanding these contexts allows for improved logging, tracing, and error handling in your SpecFlow tests. Combining this knowledge with hooks can enhance your testing capabilities.

I hope you found this information useful. I certainly learned a lot while exploring these features! 😊

Check out the code examples here.

Happy coding!

Twitter, Facebook