Behavior Driven Development Using Cucumber In Visual Studio

Introduction

Here, we will try to understand BDD, which means Behavior Driven Development, using Cucumber in Visual Studio. We already know about TDD, the Test Driven Development,  where we create unit tests and validate our code with test cases so that we can confirm that the functionality is working properly.

What is Cucumber ?

Basically, Cucumber is used for writing BDD test cases, and for writing this test cases, Gherkins is the language. This is not a programming language but a plain English statement language that is used with some keywords.

One can get more information by visiting https://cucumber.io/

Please see the below example.

Scenario

Add two given numbers. I have entered 10 into the calculator and I have entered 10 again into the calculator. When I press Add, the result should be 20 on the screen.

We can integrate any programming language with Cucumber.

Scenario

In this sample, I am trying to implement the Cucumber with .NET technologies. The development environment used is Visual Studio 2015.

One can use any other programming language, like Java, JavaScript, PHP, or Ruby.

Integration with Visual Studio

In this article, I am using Visual Studio for integration of Cucumber. Please open Visual Studio.
 
Go to Tools >> Extensions and updates. Select Online option from left tabs and search for SpecFlow.

Scenario

Click the Download option.

Scenario

Scenario

Click "Install" and restart Visual Studio.

Create Sample

Now, create sample Test Project. Select Solution Explorer and create new item. Please find the below screenshot for better understanding.

Scenario

One can create new item with .feature. This is the Cucumber item we are  able to integrate into our .NET project.  Select "Add" and continue.

Scenario

If you open the SpecFeature1.feature file, then you will be able to see the below lines of codes.

Scenario

Right click on the feature file and select "Generate Step Definitions".

Scenario

If you are not getting the option "Generate Step Definitions", then please go to Package Manager Console and paste Install-Package SpecFlow.NUnit and press ENTER. Let this package get installed properly.

Then, you will be able to see the option "Generate Step Definitions".


Scenario

Press "Generate" button. Now, open SpecFlowFeature1Steps.cs.

Scenario
  1. [Given(@ "I have entered (.*) into the calculator")]  
  2. public void GivenIHaveEnteredIntoTheCalculator(int p0) {  
  3.     ScenarioContext.Current.Pending();  
  4. }  
  5.   
  6. [When(@ "I press add")]  
  7. public void WhenIPressAdd() {  
  8.         ScenarioContext.Current.Pending();  
  9.     }  
  10.     [Then(@ "the result should be (.*) on the screen")]  
  11. public void ThenTheResultShouldBeOnTheScreen(int p0) {  
  12.     ScenarioContext.Current.Pending();  
  13. }  
This is actual C# code for the .feature file.

Now, we will call calculator class from SpecFlowFeature1Steps. One can add the required functionality in CS file and validate. Also, we can add the assets just like we add in unit testing.

Executing the test cases

Please open the Test Explorer. You will see the list of test cases. We can easily execute those, like we do in unit test.

Please find the below screenshot for better understanding.

Scenario

Advantages of BDD
  • The test scenario is easily readable. 
  • The test cases are easily understood by business users other than developers and the quality team.