Beyond the basics

Posted on:

(New to SpecFlow? Continue reading on the Getting Started page.)

Now that you have the basics down, it’s time to introduce some features you may not be aware of yet.

  • Context Injection – a clean and maintainable solution for sharing data between steps

    In SpecFlow, step definitions are global – steps defined for a single scenario may be bound to step definitions in multiple classes. Sharing data between step definitions when executing a scenario is common. For example, the given step can prepare data to be entered and the then step then validates whether the data was entered successfully into the system. If these two step definitions are in the same class, this is pretty easy to handle with instance fields. However, if they are in different classes, sharing the data is trickier. SpecFlow provides a general tag (ScenarioContext) for scenario-execution-specific data, but this does not really conform to the SOLID principles. Furthermore, if there are multiple values that need to be shared and belong together (e.g. first name and last name), this solution results in glorious spaghetti code. Context injection is here to help!

    SpecFlow has a built-in mini dependency injection framework that can resolve dependencies of the binding classes (e.g. classes containing step definitions) provided as constructor parameters. This is called context injection. While it may sound complicated, it is very easy to use: just create your POCOs representing the shared data, and define them as constructor parameters in all binding class that require them.

    You can find examples and more details in the documentation.

  • Assist helpers – smart processing of SpecFlow tables

    The Gherkin specification language used by SpecFlow supports table arguments for steps. These tables are useful when a large amount of data needs to be specified for a step, such as a person’s details. SpecFlow provides the data in these tables as a Table argument in the step definitions, which can be used as a general list of key-value pairs: the key is the column name (a string) and the value is the string entered in the table cells. Maintaining the consistency of the header strings and converting the cell values to the proper data types can be cumbersome. But the assist helpers are here to help!

    Using the assist helpers, you can convert tables into a POCO (when using key-value pair style two-column tables) or a list of POCOs with the table.CreateInstance() / CreateSet() methods, and compare a table with an object or a list of objects with the table.CompareToInstance() / CompareToSet() methods. All you have to do is add the TechTalk.SpecFlow.Assist namespace to the file.

    You can find examples and more details in the documentation. Also take a look at the SpecFlow.Assist.Dynamic extension of the assist helpers with .NET 4.0 dynamic support.

  • [StepArgumentTransformation] attribute – define pattern-based conversions for step definitions

    Parameters in step definitions can make steps more reusable. But conversions beyond the built-in ones (e.g. converting a table with person details to a PersonData class, or handling today as a DateTime) can spread the step definition methods. Don’t worry, the [StepArgumentTransformation] attribute is here to help!

    The step argument transformations can be used to apply a custom conversion step to the arguments in the step definitions. The step argument transformation is a method that converts from text (specified by a regular expression) or a Table instance to an arbitrary .NET type. The conversion method has to be marked with the [StepArgumentTransformation] attribute.

    You can find examples and more details in the documentation.

  • Handling test errors – boost your error reporting

    If you already have a bunch of executable scenarios and you are running them on your CI server, you have probably realized that detailed information about failing tests can be life-saver (this was one of the reasons why developed SpecFlow+ Runner). For example, you may want to attach a screenshot of your automated web browser to the exception trace. Let’s do it, the hooks and the ScenarioContext are here to help!

    The current ScenarioContext instance contains a lot of useful information about the currently executing scenarios, like the scenario tags, the current step block (given, when or then) and the last exception. Combining this with an [AfterScenario] hook, you can provide more details on scenarios execution failures.

    You can find examples and more details in the documentation.

Need help?


  • Do you want to deepen your understanding of SpecFlow and test automation?
  • Do you want your entire team to be well-versed in the use of SpecFlow?
  • Do you want to learn how to write Gherkin specifications that are maintainable and can be automated efficiently?
  • Do you want ensure you are heading in the right direction?

TechTalk offers training, private workshops, reviews and coaching for agile requirement management and testing, SpecFlow and test automation. Contact us for details.


  • Do you want to learn BDD and get certified by Scrum Alliance?

Check out the Certified Scrum Development course of Gaspar Nagy.

Support and customization

  • Do you want to secure SpecFlow usage with SLA-based support?
  • Did you find a blocking issue or a missing feature that you cannot fix yourself?
  • Do you want to have a customized report or a feature specific to your environment?

TechTalk offers dedicated support with guaranteed response time (see full SLA), tool customizations and prioritized implementation of features or fixes. Check out SpecFlow+ for details.

Get smarter feedback with SpecFlow+ Runner

SpecFlow+ Runner (formerly known as SpecRun) is a smart test runner that integrates with SpecFlow and Visual Studio

  • Speed up your tests using parallel execution
  • Simplify your test setup
  • Get better feedback on your test executions
  • Generate customisable test reports

Living documentation with SpecLog

SpecLog is a visual tool for managing requirements using story maps. You can define requirements and acceptance criteria, link Gherkin feature files and display the current status of test cases when using a SpecFlow server.

  • Write story cards and capture your story maps electronically
  • Supports infinite workspaces, so you will never run out of walls again!
  • Link feature files to user stories
  • Extend requirements with acceptance criteria and attachments
  • Synchronise requirements with your work item system (TFS/JIRA)
  • Build up a living documentation including your SpecFlow tests
  • Supports remote collaboration

Visit the SpecLog web site for more information.

Local services

  • Einfacher auf Deutsch? (Easier in German?)

TechTalk bietet Development und Consulting Services in der DACH Region.
(TechTalk provides development and consulting services in Germany, Austria and Switzerland).