SpecFlow Re-usable step definitions

Chris McKelt picture Chris McKelt · Mar 8, 2011 · Viewed 9.9k times · Source

Is there a way to have SpecFlow reuse Step Definitions?

In other tools I have used a GivenWhenThen base class that contains methods such as

WhenAnOrderIsCreated -- this inits a protected order member to be used by inheriting classes.

Just cant seem to get this working with SpecFlow (doesnt seem to like inheritance)

Is there a way to share steps across features?

Many thanks

Answer

Marcus Hammarberg picture Marcus Hammarberg · Mar 8, 2011

Why yes it's possible - check out the calling steps from step feature (https://specflow.org/documentation/Calling-Steps-from-Step-Definitions/)

In short you create a step definition class that inherits from Steps like this:

[Binding]
public class CallingStepsFromStepDefinitionSteps : Steps
{}

And then you can simply call other steps like this:

[Given(@"I am logged in")]
public void GivenIAmLoggedIn()
{
     Given("I am on the index page");
     When("I enter my unsername nad password");
     And("I click the login button");
     incStepCount();
}

I hope I understood your question correctly and that this was an answer to it