BDD Header
This article describes how to use the Ranorex Studio IDE and the Ranorex API for test automation in your behavior-driven development (BDD) process. BDD requires a cognitive shift: instead of thinking about testing functions, you are now thinking about behaviors. Following this approach enables seamless collaboration between business stakeholders (such as product management) and the QA or development teams. On the business side, the product management team can define feature specifications in natural language without writing a single line of code. On the technology side, the QA team and the development team add automation steps using recording modules and the repository.

Watch our on-demand webinar

Breaking Through the Barriers to BDD: Learn how to foster collaboration and improve product quality through behavior-driven software development.

Tool Stack

The diagram below shows a sample tool stack to create a BDD-based testing framework. Ranorex Studio provides the development environment, while the Ranorex test automation framework provides the automation API. To parse the feature files from natural language to written code, we are using SpecFlow as the BDD interpreter. Finally, NUnit is the unit test provider, which manages and executes the tests.

BDD Tool Stack

Process Overview

When following the BDD approach in an agile environment, the entire development process of a user story is divided into a business side and a technology side.


On the business side, we create user stories, which are taken and broken down into features stored in feature files. These features are broken down into scenarios. The features are written down in a natural language following the Gherkin syntax. A basic feature written in Gherkin might look something like this:

Feature: Serve coffee
 In order to earn money
 Customers should be able to
 buy coffee at all times

Scenario: Buy last coffee
 Given there are 1 coffees left in the machine
 And I have deposited 1 dollar
 When I press the coffee button
 Then I should be served a coffee

On the technology side, we need an interpreter to parse the feature files written in natural language (Gherkin syntax) and call test automation code. These Ranorex API calls or Ranorex recordings do the following:

  • Automate the system under test
  • Verify the expected output
  • Returning a corresponding result

Prepare Ranorex Studio for BDD

The SpecFlow Add-in for Ranorex Studio

The SpecFlow add-in provides file templates for feature files, step definition files and event definition files. It also translates the features from Gherkin syntax to C# code. To install the SpecFlow add-in to Ranorex Studio, follow the instructions below:

Download or clone the SpecFlow repo and open the solution in Visual Studio.

Open the project “TechTalk.SpecFlow.RanorexIntegration”, remove the following libs from the references:

  • ICSharpCode.Core
  • ICSharpCode.SharpDevelop
  • ICSharpCode.SharpDevelop.Dom

and replace them with the dlls from <Ranorex installation folder>bin.

Next, build the RanorexIntegration add-in.

Build add-in

Extract the built “.sdaddin” file from the “IdeIntegration” folder of the SpecFlow solution to a new folder “TechTalk.SpecFlow.RanorexIntegration“.

Copy the extracted folder to <Ranorex Installation folder>\Addins\Misc.

Make your Ranorex Solution BDD ready

Open the “Package management console” (View->Tools->Package management console) and run the following commands:

Install-Package SpecFlow.NUnit -Version 2.3.2
Install-Package NUnit.ConsoleRunner

Get started with BDD

Now that your Ranorex Solution is BDD-ready, you can begin defining features in SpecFlow and creating automated tests for them in Ranorex. You can see how to do this in the screencast “BDD and Ranorex,” or you can read the instructions below, whichever you prefer.

Define a feature from the business side

In SpecFlow, create a new feature file. To do so, right-click on the project, then choose Add > New Item > Category: SpecFlow > SpecFlow Feature.

Describe the behavior of your system using human-readable syntax in Gherkin. For Example:

Feature: Calculator
 In order to avoid silly mistakes
 As a math idiot
 I want to be told the square root of a number

Scenario: Square root of a Number
 Given I click 3 on calculator
 When I press sqrt
 Then the result should be 9 on the screen

Save the file. Ranorex Studio will create a <name>.feature.cs file containing the automatically-generated code that will execute the test scenarios using the configured test provider.

If the feature file is not created in Ranorex Studio, you have to set the “Custom tool” in the files properties to “SpecFlowFileGenerator” to make Ranorex Studio create the code file.

Set custom tool

Create step and event definitions for the technology side

Create a new step definition file. To do so, right click on the project and choose Add > New Item > Category: SpecFlow > SpecFlow Step Definition.

Now, create methods for all “Given,” “When,” and “Then” actions described in the scenario description. Add “Given”, “When” and “Then” attributes with values matching the scenario steps defined in the feature file. To do so, follow the samples provided by the template from which the new step definition file has been generated.

     public class StepDefinition1
         [Given("I have entered (.*) into the calculator")]
         public void GivenIHaveEnteredSomethingIntoTheCalculator(int number)

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

         [Then("the result should be (.*) on the screen")]
         public void ThenTheResultShouldBe(int result)

SpecFlow searches for the step definition text in the step definition files that matches the text in the scenario step.

BDD Code Binding

Now you can implement the automated step, either by creating recording modules or by manually implementing the steps using the Ranorex automation API.

Your implementation might look something like this:

     public class sqrt
         [Given("I have entered (.*) into the calculator")]
         public void GivenIHaveEnteredSomethingIntoTheCalculator(int number)
             ClickNumButton.repo.numButton = number.ToString();

         [When("I press the square root button")]
         public void WhenIPressSqrt()

         [Then("the result should be (.*) on the screen")]
         public void ThenTheResultShouldBe(int result)
             ValidateResult.Instance.result = result.ToString();

You can create additional steps to be executed before and after a scenario, such as performing setup and tear down tasks, including starting and closing the system under test.
These steps can be defined in an event definition file and might look something like this:

         public void BeforeScenario()

         public void AfterScenario()

Make the project ready for the unit test provider

After implementing the automation steps bound to the feature description, you can build your project as a library so that it can be passed to NUnit. To do so, open the project properties and set the output type of the project to “class library”. In addition, copy all Ranorex runtime DLLs to the output folder.

Build as lib

Run a BDD test using NUnit

Because this solution uses NUnit as the unit test provider, we use a NUnit Runner to execute our tests.
Your command line call should look something like this:

..\..\..\packages\NUnit.ConsoleRunner.3.9.0\tools\nunit3-console.exe <solution>.dll

Now the steps defined in the feature file will be executed and the results will be shown in the terminal. See the result of your test run for detailed information about the performed steps.


The preparation of Ranorex for BDD is complete. From this point forward, your business team can define user stories in a natural language following the Gherkin syntax, and your development and QA team can implement the automation steps in the step definition files.

You might also like these articles