Adding Specflow to a Visual Studio project

I’m using Visual Studio Community 2015 and want to write some acceptance tests – here are the quick steps to get started. Most of these can be found on the SpecFlow getting started page.

High level steps

  1. Install a Visual Studio Plugin so you don’t have to write too much SpecFlow glue code
  2. Write your features and steps
  3. Run your tests in your favourite test runner

Detailed steps:

  1. Install SpecFlow for Visual Studio
    This is a .vsix extension for Visual Studio 2015. There should be a version for your Visual Studio. This adds shortcuts such as feature files and custom tools, which means when you change your feature files, your feature.cs files are automatically generated upon each build. If you don’t have this, you’re probably going to have a long process of manually creating those files instead.
  2. Restart Visual Studio after the install so you get all the tools from the SpecFlow extension
  3. Create a test project and install the SpecFlow nuget
    Install-Package SpecFlow.NUnit

  4. Add a new .feature file to describe your feature in Cucumber language syntax. Right click and choose Add Item. Choose “SpecFlow Feature”. These templates were added by the .vsix Visual Studio Extension.When you add the item, you’ll see the feature file has been added, along with it’s glue – the .feature.cs file. This file is generated automatically by SpecFlow and joins the feature file to the any separate files. This file is generated at each build (or when you click Run custom tool on the .feature file), so don’t put your code for your steps in here, instead create another file.Here’s my example .feature file:
    Feature: InstancesFromTemplates
    	In order run new flows
    	As a user
    	I can create a new flow instance from a flow template
    Scenario: Do something example
    	Given I setup something
    	And I setup something else
    	When I do an action
    	Then I check the result
  5. Create a file for your steps. This is going to be a blank C# file with a [Binding] attribute on top so SpecFlow can discover it.
    using TechTalk.SpecFlow;
    namespace Flow.WebApi.Tests
        public class Steps
            // steps will go here
  6. Write a few steps in your feature file and generate the steps by right clicking the feature lines in the .feature file and clicking Generate Steps.
  7. Copy the generated steps into the clipboard and paste them into the Steps.cs file.
  8. Your steps file will look like this:
    using TechTalk.SpecFlow;
    namespace Flow.WebApi.Tests
        public class Steps
            [Given(@"I setup something")]
            public void GivenISetupSomething()
                // setup
            [Given(@"I setup something else")]
            public void GivenISetupSomethingElse()
                // setup something else
            [When(@"I do an action")]
            public void WhenIDoAnAction()
                // act
            [Then(@"I check the result")]
            public void ThenICheckTheResult()
                // assert
  9. Now when you rebuild, because you’ve got a Specflow [Binding] attribute on that class which makes it discoverable, your steps will be hit and your tests will be run in your runner.
    Rebuild your project, and run your tests:


Running Powershell as an Administrator

From Powershell:

Start a new Powershell process as an Administrator from an existing Powershell command:

Start-Process powershell -Verb runAs

Or with arguments:

Start-Process powershell -Verb runAs -ArgumentList <Arguments>

From the command line:

Run the following from a non-powershell command line:

powershell -Command "Start-Process PowerShell –Verb RunAs"


Performance Counters with NLog

I spotted that NLog have a performance counter target and decided to give it a whirl.

I’m currently working heavily with windows performance counters using a framework called PerfIt! and wanted to see if there was an easier (to configure) way.

Grab the source code here:

Install-Package NLog
Install-Package NLog.Config
Install-Package Topshelf

Using the topshelf getting started guide I created a quick example app which would write an INFO level log every second.

public class Program
  public static void Main()
    HostFactory.Run(x =>
      x.Service<Service>(s =>
        s.ConstructUsing(name => new Service());
        s.WhenStarted(tc => tc.Start());
        s.WhenStopped(tc => tc.Stop());

      x.SetDescription("Ex Topshelf Host");

public class Service
  readonly Timer _timer;
  private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
  public Service()
    _timer = new Timer(1000) { AutoReset = true };
    _timer.Elapsed += (sender, eventArgs) => _logger.Info("This is an info log message");
  public void Start() { _timer.Start(); }
  public void Stop() { _timer.Stop(); }

The real magic happens in the target file. This is where the counters are created using the
autoCreate flag.

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="" xmlns:xsi="" xsi:schemaLocation=" NLog.xsd" autoReload="true" throwExceptions="true" internalLogLevel="Info" internalLogFile=".\nlog-internal.log">

     counterHelp="This was created by NLog"
     incrementValue="1" />

   <target xsi:type="Console" name="console" detectConsoleAvailable="true" />

   <logger name="*" minlevel="Debug" writeTo="perf" />
   <logger name="*" minlevel="Debug" writeTo="console" />

When you put this all together, you get automatically created Performance Counters (available after you’ve restarted your console of course).

PS> Get-Counter -ListSet *example*

CounterSetName     : ExampleCategoryName
MachineName        : .
CounterSetType     : SingleInstance
Description        : Category created by NLog
Paths              : {\ExampleCategoryName(*)\Alexw.Example}
PathsWithInstances : {\ExampleCategoryName(logwritten)\Alexw.Example}
Counter            : {\ExampleCategoryName(*)\Alexw.Example}