What is the best way to reuse SpecFlow Given/When/Then steps? I figured out three ways with all specific advantages and drawbacks, but I am not convinced this is the best way.
I have two projects in one solution
ProjectA:
[Binding]
public class BookSteps : StepsBase
{
[Given(@"the following books:")]
public void GivenTheFollowingBooks(Table table)
{
// ...
}
}
ProjectB:
[Binding]
public class BookStepsReference : ProjectA.BookSteps { }
This works and requires the least work. Unfortunately it breaks the intellisense of the feature files: The steps stay purple in the ProjectB feature files.
ProjectB:
[Binding]
public class BookStepsReference : ProjectA.BookSteps
{
[Given(@"the following books:")]
public void GivenTheFollowingBooksReference(Table table)
{
base.GivenTheFollowingBooks(table);
}
}
This breaks when I try to run the test because the auto-generated feature steps sees two methods with a Given attribute "the following books:" and throws an ambiguous reference exception.
ProjectB:
[Binding]
public class BookStepsReference
{
private ProjectA.BookSteps _bookSteps = new ProjectA.BookSteps();
[Given(@"the following books:")]
public void GivenTheFollowingBooks(Table table)
{
_bookSteps.GivenTheFollowingBooks(table);
}
}
This works and also applies the correct intellisense on the Feature File steps. But when I want to debug my steps, I get an external COM exception on the initializing of the _baseSteps object, probably caused by the SpecFlow library that sees a double Binding attribute.
The last option is the way I am working right now, but I'd like to know if others have created a better way to reuse steps from other projects.
The supported way is to add a reference to the project which has the steps you want to reuse into the project you want to use then in and then define an external step assembly in the config like this:
<specFlow>
<stepAssemblies>
<stepAssembly assembly="MyAssembly.Name" />
</stepAssemblies>
</specFlow>
If you need to only reuse some steps then you might need to extract those steps into a separate library first.
When you add stepAssembly (see below) to config then intellisence can`t see steps from stepAssembly. See here how delete specFlow cache and allow this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With