Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse SpecFlow steps in another project in the same solution

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)
    { 
        // ...
    }
}
  1. I can just inherit my steps like this:

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.

  1. I can inherit and create a method with the same signature:

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.

  1. I make a private object that references the binding steps from the project A:

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.

like image 689
AutomatedChaos Avatar asked Mar 30 '15 15:03

AutomatedChaos


2 Answers

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.

like image 80
Sam Holder Avatar answered Nov 15 '22 17:11

Sam Holder


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.

like image 42
Александр Шабунин Avatar answered Nov 15 '22 16:11

Александр Шабунин