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 makes the solution a bit hackish. Sorry for that.
SpecFlow stores 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 several zip archives containing the templates. You can modify the EventDefinition, StepDefinition, and (maybe most commonly) the Feature template. All the structure is the same, so you can follow it in the same manner as described below.
How to Modify the Feature Template
- Make a copy of the current template (
SpecFlowFeature.zip
) to be safe. - Extract the file in
SpecFlowFeature.zip
to a new folder. - Open the
SpecFlowFeature1.feature
file in Visual Studio. - Now you can edit it as much as you want. In our case, we wrote some instructions as comments, converted the text into Swedish (our standard language in
app.config
), and wrote some common background steps. - Save the file.
- Create a new zip archive with just the three 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.
- A restart of Visual Studio might help in picking up the new template. It doesn’t hurt to restart it.
Multiple Templates to Choose From
You can take this a step further. If the ItemTemplates
folder contains several definitions for a certain template (EventDefinition, StepDefinition, or Feature), they will show up in your Add New Item dialog:
The only thing is that they have the same name. But that’s what the 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 my Marcus version of a SpecFlow feature:
Important elements in this file include:
- Name – The name that will show up in the Add New Item dialog.
- Description – Displayed to the right in the Add New Item dialog, providing additional information like which project this template is used in.
- Icon – You could change the icon for the file. For example, an Avega branded feature file.
- 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 correct file name of the .feature
file in the zip archive, you get the following glorious Add New Item dialog:
When we add it, we see that the Marcus version held nothing much new:
… but I’m sure you get the idea. This can be tweaked to match your project or team exactly (language, default scenarios, background steps).
Conclusion
In this post, I showed two ways to customize 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 find it useful. Thank you again, Dejen, for pointing me this way.