View on GitHub

macro-specflow

Plugin will replace any substring from your step definition with custom value.

Build status

Example

Create scenario

Given I use macro _today_
Then The value of _today_ shuld be the same

for one test scenario the value will be replaced with body of your function

Given I use macro 20/03/2018
Then The value of 20/03/2018 shuld be the same

Installation

Install Macro.SpecFlow NuGet package into your project with scenarios.

PS> Install-Package Macro.SpecFlow

After installing NuGet package your App.config is modified. Macro plugin will be registered in the specFlow section.

<specFlow>
  ...
  <plugins>
    <add name="Macro" type="Runtime" />
  </plugins>
  ...
</specFlow>

Configuration

All settings are stored in Macro.SpecFlowPlugin.dll.config file which was added into your project by nuget installation.

Property Description
assembly - name name of the library where to search macro
  <configSections>
    <section name="macroSpecFlow" type="Macro.SpecFlowPlugin.Config.MacroSpecFlowSection, Macro.SpecFlowPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </configSections>
  <macroSpecFlow>
    <assemblies>
      <assembly name="Project.Assembly.dll"/>
    </assemblies>
  </macroSpecFlow>

Custom macro

You can implement your own macro which will be used for replacing values. Add a class which implement IMacro interface and add attribute Pattern with your custom pattern. Now plugin will use your pattern while eunning.

[Pattern("_today_")]
class TodayMacro : IMacro
{
    public string Process(Match match)
    {
        return DateTime.Now.ToString("d");
    }
}

[Pattern(@"_random\((?<input>\d+)\)_")]
class RandomMacro : IMacro
{
    public string Process(Match match)
    {
        var input = int.Parse(match.Groups["input"].Value);
        return new Random().Next(input).ToString();
    }
}