BDD In C# Using SpecFlow

Introduction

BDD (Behavior driven development) adapted features of TDD(Test Driven Development) and extended a few syntaxes, which made the Product Owner (PO Role of Scrum Process) or Business Analyst job easy to convert requirements into standard scripts, by analysing the behaviour of features required by customers. And the developer's job will be easy with this behavior, scripts drive the development.

We will achieve this in C# using SpecFlow extension.

Install the SpecFlow as shown in the image below, Go to Extension --> Manage Extension --> Online and type specflow

BDD in C# using SpecFlow

We will try to achieve this with serialization and deserialization of samples.

Create specflow project as shown in images below with targeted framework .Net 6.0 and Testing framework Nunit (you can choose your convenient testing framework)

BDD in C# using SpecFlow

Add feature file in Features folder like below and update the feature, scenario, given, when, then like below (2 features file created)

//Filename: Serialization.feature
Feature: Serialization
A short summary of the feature

Scenario: Serialize object to json
	Given a person object
	When Serialize method is callled
	Then get the object in json format

//Filename: Deserialization.feature
Feature: Deserialization
A short summary of the feature

Scenario: DeSerialize json to object
	Given a person object in string format
	When DeSerialize method is callled
	Then get the json in person object

To generate a step definition for the above scripts, right click and select "Define steps" like below, which will popup Skelton for the given, when, then step methods, copy that add it in StepDefinations folder like below for both the files. and followed by providing the implementation.

BDD in C# using SpecFlow

// Filename: SerializationSteps.cs
using NUnit.Framework;
using Serialization.Deserialization;
namespace SampleSpecFlowProject.StepDefinitions {
    [Binding]
    public class SerializationSteps {
        Person ? person;
        Serialization_Deserialization ? serialization_Deserialization;
        string ? result;
        [Given(@ "a person object")]
        public void GivenAPersonObject() {
                person = new Person {
                    Name = "ABC",
                        Age = 20,
                        Gender = "Male",
                        IsStudent = true
                };
                serialization_Deserialization = new Serialization_Deserialization();
            }
            [When(@ "Serialize method is callled")]
        public void WhenSerializeMethodIsCallled() {
                result = serialization_Deserialization.Serialize(person);
            }
            [Then(@ "get the object in json format")]
        public void ThenGetTheObjectInJsonFormat() {
            string expected = "{\r\n  \"Name\": \"ABC\",\r\n  \"Age\": 20,\r\n  \"Gender\": \"Male\",\r\n  \"IsStudent\": true\r\n}";
            Assert.AreEqual(result, expected);
        }
    }
}
using NUnit.Framework;
using Serialization.Deserialization;
//Filename: DeserializationStepDefinitions.cs
using System;
using TechTalk.SpecFlow;
namespace SampleSpecFlowProject.StepDefinitions {
    [Binding]
    public class DeserializationStepDefinitions {
        string jsonText = string.Empty;
        Serialization_Deserialization serialization_Deserialization = null;
        Person result = null;
        [Given(@ "a person object in string format")]
        public void GivenAPersonObjectInStringFormat() {
                jsonText = "{\"Name\":\"ABC\",\"Age\":20,\"Gender\":\"Mail\",\"IsStudent\":true}";
                serialization_Deserialization = new Serialization_Deserialization();
            }
            [When(@ "DeSerialize method is callled")]
        public void WhenDeSerializeMethodIsCallled() {
                result = serialization_Deserialization.Deserialize < Person > (jsonText);
            }
            [Then(@ "get the json in person object")]
        public void ThenGetTheJsonInPersonObject() {
            Assert.IsNotNull(result);
        }
    }
}

Solution Explorer looks like below,

BDD in C# using SpecFlow

Once all the above steps are done. goto view--> Test Explorer --> you should be able to see 2 test cases like below,

BDD in C# using SpecFlow

Create console project with targeted framework .Net 6.0

Below is the code files for serialization and deserialization. Here to develop this, we need to follow TDD. As the article is to know about BDD. I am putting code directly.

//Filename: Serialization_Deserialization.cs
using Newtonsoft.Json;
namespace Serialization.Deserialization {
    public class Serialization_Deserialization {
        public string Serialize(object obj) {
            string json = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
            return json;
        }
        public T Deserialize < T > (string jsonText) {
            T obj = JsonConvert.DeserializeObject < T > (jsonText);
            return obj;
        }
    }
}
namespace Serialization.Deserialization
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Gender { get; set; }
        public bool IsStudent { get; set; }
    }
}

Execute the test case by right clicking on it and fix it, if you have issues.

BDD in C# using SpecFlow

Summary

In this article we understood the use of BDD along with specFlow, hope it helps. FYR I have attached a project, and make use of it.


Recommended Free Ebook
Similar Articles