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.
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.
Click the Download option. 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. 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. If you open the SpecFeature1.feature file, then you will be able to see the below lines of codes. Right click on the feature file and select "Generate Step Definitions". 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". Press "Generate" button. Now, open SpecFlowFeature1Steps.cs. - [Given(@ "I have entered (.*) into the calculator")]
- public void GivenIHaveEnteredIntoTheCalculator(int p0) {
- ScenarioContext.Current.Pending();
- }
-
- [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();
- }
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. 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.