Specflow -Part1(Installation and Introduction)

We have already talked about BDD in our course introduction, it is a specification used for software development

Specflow is a testing framework that supports BDD and uses Gherkin language to define application behaviour

You can refer to course introduction for more details on BDD, Specflow and Gherkin

We will jump into using specflow

Installation

In visual studio,

  1. Go to Tools--> Extensions and updates
  2. Select online and in search type specflow
  3. Install Specflow for visual studio 
We have added the specflow extension for visual studio, let's add the specflow package to our project


  1. Create a new solution and add test project called SpecflowTest
  2. Right-click the project and select 'Manage Nuget packages'
  3. Search for specflow.mstest and install
In App.config file of the project,

Verify if below tag is present inside specflow tag


<unitTestProvider name="MsTest" />


If not, then go ahead add same.
Here we are telling specflow to use MsTest unit testing framework under the hood.
Specflow also supports other testing frameworks like NUnit.

Now let's add feature file in our test project
  1. Right-click the project-->Add-->New item
  2. Select Specflow Feature File
  3. Click on Add
You can notice SpecflowFeature1.feature file is added to the project


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


We will now talk about the above feature file

Feature file starts with Feature keyword followed by name of the feature.
Also, the next line is a short description of the feature we are going to test.

A scenario is a test we are going to write to test the feature and it has Steps in Given When Then format

Given-->Arrange
When-->Act
Then-->Assert

 @myTag is Tag for the scenario, we can add our tags like @Smoke @BAT or similar for scenarios or feature to add more details to them.

Now, under test explorer, you can see SpecflowFeature1Feature scenarios can be seen, in our case AddTwoNumbers.

Right-click the AddTwoNumbers test and click on Run Selected Tests, it will fail giving below error

No matching step definition found for one or more steps.

This is fair enough, as we haven't implemented the steps , notice the colour of the steps in the above feature file.

Steps in purple colour tell the user that we do not have step definitions or the code which should run for step execution.

We will now add code for our first Given Statement, to do so, follow below
  1. Right Click anywhere in feature file and select Generate Step Definitions
  2. Check only first Given checkbox and click generate
  3. Browse the current project directory and click on save
This will create SpecFlowFeature1Steps.cs file with step definitions


namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int p0)
        {
            ScenarioContext.Current.Pending();
        }
    }
}


Also t,he feature file now looks like below


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




Notice the first two steps the text colour is now white, you can also notice that it has created a single method in SpecFlowFeature1Steps.cs file for these 2 steps.

Steps are pretty same and only the integer value is changing which is parameterized in the method

Now we will write code for this method which will serve as implementation for the first two steps


namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
        private List<int> inputList = new List<int>();
 
 
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int number)
        {
            inputList.Add(number);
 
        }
    }
}


We created a list to hold the integer values and upon executing the step the number provided by the user is added to list.

Similarly, we will add step definitions for other remaining steps 

  1. Right Click anywhere in feature file and select Generate Step Definitions
  2. Select all checkbox and click copy methods to the clipboard
  3. Open SpecFlowFeature1Steps.cs and paste the definations
So our SpecFlowFeature1Steps.cs looks like one below

namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
 
        private List<int> inputList = new List<int>();
 
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int number)
        {
            inputList.Add(number);
 
        }
 
        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            ScenarioContext.Current.Pending();
        }
 
 
        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int p0)
        {
            ScenarioContext.Current.Pending();
        }
    }
}



We will implement the other 2 steps as below

namespace SpecflowTest
{
    [Binding]
    public class SpecFlowFeature1Steps
    {
 
        private List<int> inputList = new List<int>();
        int sum = 0;
 
        [Given(@"I have entered (.*) into the calculator")]
        public void GivenIHaveEnteredIntoTheCalculator(int number)
        {
            inputList.Add(number);
 
        }
 
        [When(@"I press add")]
        public void WhenIPressAdd()
        {
            foreach (int number in inputList)
                sum = sum + number;
 
        }
 
 
        [Then(@"the result should be (.*) on the screen")]
        public void ThenTheResultShouldBeOnTheScreen(int total)
        {
            Assert.AreEqual(total, sum, "Total does not match");
        }
    }
}

In when step, we added the numbers from the list and stored in a local variable called sum.
In Then step, we asserted that the value matches with one we are expecting.

Run the test and you should get a pass.

That's all for this lecture 👱


Comments

Popular posts from this blog

Specflow -Part3(Working with tables using Specflow.Assist.Dynamic)

Specflow -Part4(Specflow Scenario Outline and Feature Background )

Specflow -Part6(Specflow Hooks)