7

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.

3 Answers 3

16

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.

1
  • Ah, forgot about that. It seems I can just use the same setup as I have now and only have to enter the above lines in my configuration. Mar 31, 2015 at 7:31
3

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.

1

If you are using Specflow 3, this is the updated link https://docs.specflow.org/projects/specflow/en/latest/Bindings/Use-Bindings-from-External-Assemblies.html

specflow.json example:

{
  "stepAssemblies": [
    {
      "assembly": "MySharedBindings"
    }
  ]
}

app.config example:

<specFlow>
  <stepAssemblies>
    <stepAssembly assembly="MySharedBindings" />
  </stepAssemblies>
</specFlow>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.