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
- Make a Copy of the current template (SpecFlowFeature.zip) just to be sure
- Extract the file in SpecFlowFeature.zip a new folder
- Open the SpecFlowFeature1.feature file in Visual Studio
- Now you can edit the crap out of that thing . 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
- Save the file
- 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:
- 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.
- 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:
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:
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:
And when we add it – we see that the Marcus version held nothing much new…
… 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).
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.