Advanced tips for SpecFlow and Behavior-Driven Development in .NET

NET category image

We are going to look at things like ScenarioContext, setup and teardown, navigation inside the IDE, and using arguments in step definitions.

You may have seen my other post about Behavior-Driven Development with .NET, which is a tutorial on how to set it up for you. In this article, I am going to give further tips beyond the basics that will increase your ability to write good tests and debug failing tests faster.

Using ScenarioContext/FeatureContext

The ScenarioContext is a state object that lives as long as a scenario runs. You can use it as a Dictionary<string, object> to pass data between steps, so you don’t need to define private fields in your step classes. To access the ScenarioContext, inject it into the binding class and save a reference to it.

A useful property of the ScenarioContext is the CurrentScenarioBlock property. It allows you to distinguish between a Given, When, and Then block. There is a similar object FeatureContext, which is basically the same as the ScenarioContext, but it lives as long as the feature runs. If you have data that are relevant for several scenarios, then you can use FeatureContext.

Setup and TearDown possibilities

A typical unit test framework like MsTest or NUnit allows you to define setup and teardown methods per test and per test class. SpecFlow offers the same features, they are called Hooks.

To prepare a database you’d probably choose the [BeforeTestRun] and [AfterTestRun] attributes. Before every feature or scenario you maybe want to reset the data, so you’d go with the [BeforeFeature]/[BeforeScenario] and [AfterFeature]/[AfterScenario] attributes.

Working with the IDE

The SpecFlow extension adds some nice features that you should use to be more productive.

  • When you click in a line of your feature file and hit F12 (or CTRL + Click), the IDE jumps right to the binding method.
  • To find all references of a binding method, right-click inside the method and select Find Step Definition Usages.
  • Lines in feature files without a binding method have a different text color. Right-click in the editor, select Define Steps and you can generate the methods.
  • You can put breakpoints in feature files and your test execution will stop before that step is executed.

Using several arguments in a step definition

To pass multiple values as arguments to a step definition, you need to define a DataTable in your feature file. See the example below.

Running a scenario with different arguments

To run a scenario several times with different arguments, you need to define a ScenarioOutline. Every placeholder must match a column in the Examples table. See the example below.

The Scenario will be run for every row in the table. So if you need more test cases, just add another row.

Conclusion

These are my advanced tips to boost your productivity with SpecFlow when writing tests.


Want More Flutter Content?

Join my bi-weekly newsletter that delivers small Flutter portions right in your inbox. A title, an abstract, a link, and you decide if you want to dive in!