SpecFlow: 00 - Quick Setup & Test

  • Getting started with SpecFlow begins with setting up the environment & getting proof of concept working
  • Install VS2017, SpecFlow IDE Extension
  • Add Project
  • Install NuGet Packages for SpecFlow, NUnit & NUnit3 Test Adapter
  • Add SpecFlow Feature File
  • Generate Test Definitions
  • Replace method statement stubs with Console.Writeln() statements
  • Build project & Run Test
  • Ensure Test Passed & produced output

VisualStudio Setup

Install Visual Studio 2017 & Dependencies

SpecFlow IDE Plugin

Tools > Extensions and Updates

Search for SpecFlow & Install

SpecFlow NuGet

References > Manage NuGet Packages

Search for SpecFlow & Install

NUnit NuGet

References > Manage NuGet Packages

Search for NUnit & Install

Search for NUnit2 Test Adapter & Install

Build project to seat the packages & check for preliminary errors before changing anything

SpecFlow Feature File

Add Item > SpecFlow Feature File

keep default name

Feature: SpecFlowFeature1
    In order to avoid silly mistakes
    As a math idiot
    I want to be told the sum of two numbers

@mytag
Scenario: Add two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120 on the screen

 

SpecFlow Test Definitions

Right click inside the feature file

Select Generate Definitions

Replace the following in each method with a simple Console.Writeln statement

ScenarioContext.Current.Pending();

The default statement will cause the runner to abort/fail & direct you to update the definintion methods.

This just simply gets the NUnit runner to run successfully

Build project to seat the packages & check for preliminary errors before changing anything. This will also populate the NUnit Test Explorer with the test if it isn't there already

using System;
using TechTalk.SpecFlow;

namespace SpecFlowDemo
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            //ScenarioContext.Current.Pending();
            Console.WriteLine("Entering into Calc");
        }
        
        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            //ScenarioContext.Current.Pending();
            Console.WriteLine("Pressing Add Button");
        }
        
        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int p0)
        {
            //ScenarioContext.Current.Pending();
            Console.WriteLine("Result Shows");
        }
    }
}

Test Explorer Execution

In Test Explorer, click Run, Run All, or

Right click the test and select Run...

Click on the Output link at the bottom of the Test Explorer to see results
 

Tags