-
Notifications
You must be signed in to change notification settings - Fork 0
Plugins
##Introduction##
SpecFlow is providing an improved plugin infrastructure for customization. You can implement SpecFlow plugins that can change the behavior of the built-in generator and runtime components. A typical plugin can for example provide support for a new unit testing framework.
To use a custom plugin it has to be enabled in the configuration of the SpecFlow project:
<specFlow>
<plugins>
<add name="MyPlugin" />
</plugins>
</specFlow>##Creating plugins##
Creating a plugin is fairly simple. In order to create a SpecFlow plugin you need the following three things.
- A SpecFlow.CustomPlugin Nuget package added to the library that will contain the plugin.
- A class that implements
IGeneratorPlugininterface (which is defined in TechTalk.SpecFlow.Generator.Plugins namespace) - An assembly level attribute
GeneratorPluginpointing to the class that implementsIGeneratorPlugin
Let's analyze all of these steps in detail.
I will advise you start a new class library for each plugin you intend to create. Once you create your class library as a first thing you should add the SpecFlow.CustomPlugin Nuget package to your project.
Once this is done, you need to define a class that will represent your plugin. For this class in order to be seen as a SpecFlow plugin, it needs to implement the IGeneratorPlugin interface.
By implementing the IGeneratorPlugin interface you are obliged to specify the following methods:
- RegisterConfigurationDefaults – If you are planning to intervene at the SpecFlow configuration, this is the right place to get started.
- RegisterCustomizations – If you are extending any of the components of SpecFlow, you can register your implementation at this stage.
- RegisterDependencies – In case your plugin is of a complex nature and it has it’s own dependencies, this can be the right place to set your Composition Root.
As an example, if you are writing a plugin that will act as unit test generator provider, you are going to register it in the following way, inside RegisterCustomizations method:
public void RegisterCustomizations(ObjectContainer container, SpecFlowProjectConfiguration generatorConfiguration)
{
container.RegisterTypeAs<NyNewGeneratorProvider, IUnitTestGeneratorProvider>();
}In order for your new library to be picked up by SpecFlow plugin loader, you need to flag your assembly with the GeneratorPlugin attribute. This is an example of it, taking in consideration that the class that implements IGeneratorPlugin interface is called MyNewPlugin.
[assembly: GeneratorPlugin(typeof(MyNewPlugin))]Aside of this there is another think that you need to take care of. Your assembly name needs to follow a convention, that is, it needs to have a suffix ".SpecFlowPlugin".
##Configuration details##
In order to load your plugin, in your SpecFlow project, you need to reference your plugin in the app.config file without the ".SpecFlowPlugin" suffix. It is also handy to know that the path attribute considers that project root as a path root. The following example is used to load a plugin assembly called "MyNewPlugin.SpecFlowPlugin.dll" that is located in a folder called "Binaries" that is at the same level of the current project.
<specFlow>
<plugins>
<add name="MyNewPlugin" path="..\Binaries" />
</plugins>
</specFlow>