Customize your SpecFlow templates and more

· February 1, 2011

I got this tip from a non-developer. An intern actually. He’s brilliant though and figured this out by himself, about 3 months after I introduced him to SpecFlow. Thank you Dejen – you’re great!

The fact that this is not documented anywhere just adds to the coolness, but also make the solution a bit hackish. Sorry for that.

As it seems – SpecFlow is storing templates for the feature-files in this folder:

C:\Program Files (x86)\TechTalk\ SpecFlow\ItemTemplates\

I’m using a 64-bit system so your path may not contain the (x86)-part.

Here you’ll find a number of zip-archives that contains the templates. You can modify the EventDefinition- (Hooks) and the StepDefinition-templates and (maybe most common) the Feature-template. All the structure is the same so you can follow it in the same manner as below.

How to modify the Feature template

  1. Make a Copy of the current template (SpecFlowFeature.zip) just to be sure
  2. Extract the file in SpecFlowFeature.zip a new folder
  3. Open the SpecFlowFeature1.feature file in Visual Studio
  4. Now you can edit the crap out of that thing Winking smile. In our case we wrote some instructions as comment, converted the thing into Swedish (our standard language in app.config) and wrote some common background steps
  5. Save the file
  6. Now (and here’s the trick) create a new zip-archive with just the 3 files in it. Not the folder – that won’t work. Like this: zipping together your template
  7. And you are done. From Add new item in Visual Studio you can now use your new template. And distribute it to your team-members to use.
  8. I’m not sure – but a restart of Visual Studio will not hurt you. The new template is picked up but sometimes… Restart it now. No questions asked.

###

Multiple templates to chose from

Actually we can take this a step further. If the ItemTemplates-folder contains several definitions for a certain template (EventDefiniton, StepDefintion or Feature) they will show up in you Add new item dialog. Like this:

showing 2 feature templates

The only thing is that they have the same name. But that what that other file in the zip-archive is for. It’s called “SpecFlowFeature.vstemplate” and contains information about your template. Here I’ve tweaked it to be the my Marcus version of a SpecFlow feature:

vstemplate for Marcus feature

The important stuff in this file is the following:

  • Name – the name that will show up in the Add New Item dialog
  • Description – showed to the right in the Add New item dialog. Additional information in other words. Maybe which project that this template is used in etc.
  • Icon – yes you could potentially change the icon for the file. An Avega branded feature file for example
  • ProjectItem – this element points to the file where the template is stored, so be sure to match it with the .feature file in your zip-archive.

With the above information in place, and the (%&%”#!!!) right file name of the .feature-file in the zip-archive you get the following glorious Add new item dialog:

marcus version showing up in add new item

And when we add it – we see that the Marcus version held nothing much new…

marcus feature in full swing

… but I’m sure you get the idea. This could be tweaked into something that match your project or team exactly (language, default scenarios, background steps).

Conclusion

In this post I showed two ways to use the possibility to tweak and change the item templates for SpecFlow features. The same can be done for Step definitions and Event definitions files.

Since this is pretty much undocumented (I’ve never seen anything about this) I hope you (as I did) find this useful. Thank you again Dejen for pointing me this way.

Twitter, Facebook