.

Getting started with SpecFlow in Visual Studio

I use VS 2013 for this example.

1) Create a new blank project (just a class library)

2) Install SpecFlow for Visual Studio from the menu (Tools -> Extensions & Updates)

3) right click -> add new item -> SpecFlow Feature File
Just name is “MyFirst.feature” for now.

4) just copy and paste the following in your editor window:

Feature: MyFirst
	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 + 60 into the calculator
	When I press add
	Then the result should be 110 on the screen

Scenario: Add other numbers
	Given I have entered 6 + 8 into the calculator
	When I press add
	Then the result should be 14 on the screen

5) Right click on the file content and select “generate step definitions”.
use the style = regular expressions in attributes
and simply copy the methods to clipboard

6) create a new file ( just a plain class) and name it MyFirstSteps.cscopy and paste the following code (just a very simple basic implementation as example):

using System;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace specflow.tests
{
    [Binding]
    public class MyFirstSteps
    {
        Calc c = new Calc();
        [Given(@"I have entered (.*) \+ (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0, int p1)
        {
            c.add(p0, p1);
        }

        [When(@"I press add")]
        public void WhenIPressAdd()
        {
        }

        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int p0)
        {
            Assert.AreEqual(p0, c.answer);
        }

        internal class Calc
        {

            internal void add(int p0, int p1)
            {
                answer = p0 + p1;
            }

            public int answer { get; set; }
        }
    }
}

7) if you use ReSharper, you can go to tools -> options -> specflow -> general, and change the test run a tool to ReSharper

8) Click Resharper -> Unit tests -> Run All Tests From Solution (Ctrl+U, Y)
This doesn’t do much: right click the specflow file and select “run specflow scenarios”

What's your thoughts on this?

*

Protected by WP Anti Spam