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

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

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.

How to use Behaviour-Driven Development (BDD) with C# and SpecFlow Link to heading

Here is an introductory guide on how to use Behaviour-Driven Development (BDD) with C# and SpecFlow in a .NET application.

Using ScenarioContext/FeatureContext Link to heading

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 Link to heading

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 Link to heading

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 Link to heading

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 Link to heading

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 Link to heading

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

I tried the new C# 11 features - Here are my useful takeaways Link to heading

.NET Conf 2022 is over and Microsoft released .NET 7 together with C# 11. Here are the new features and my opinion of them.

Here are my Top 5 Naming Patterns for Unit Tests in C# Link to heading

Naming tests is an underrated part of the testing process. Here are 5 patterns that you can use to keep your test names consistent.

How to implement Shell navigation, data-binding, and dependency injection in .NET MAUI Link to heading

Here is a short guide to get you started with the basics in .NET MAUI. Navigate between pages, bind data in XAML, and add dependency injection quickly.