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.
Please be aware incorporating BDD with Ranorex Studio is still in the experimental stage. If you require assistance, reach out to our support team or post a question in the user forum.
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, MSTest is the unit test provider, which manages and executes the tests.
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.
If you have installed Ranorex in “C:Program Files (x86)Ranorex 8.0”, the correct references will be added automatically.
If not, open the project “TechTalk.SpecFlow.RanorexIntegration”, remove the following libs from the references:
- ICSharp.Core
- ICSharp.SharpDevelop
- ICSharp.SharpDevelop.Dom
and replace them with the dlls from <Ranorex installation folder>bin.
Next, build the RanorexIntegration 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>AddinsMisc.
Make your Ranorex Solution BDD ready
First, download the SpecFlow 1.9.0 NuGet package file.
Next, open Ranorex Studio and create a new solution. From the main menu, select Tools > Options. Open the Package management settings, select Package sources, and add a local package source referring to your download folder.
Click OK to close the Options window.
Use the NuGet package manager to add SpecFlow v1.9.0 from your local package source, as follows: Open a solution, then select Project > Manage Packages. Click the Available tab. Use the drop-down to change the source to your Downloads folder. Click the Add button to install SpecFlow. To confirm that it is installed, click the Installed tab. To close the Manage Packages dialog, click Close.
Open the app.config file in Ranorex Studio. In the SpecFlow section, change the unit test provider to fit your needs. In our approach, we will use MSTest (<unitTestProvider name=”MsTest” />), but you can use other providers, such as NUnit.
For a full list of available unit test providers, refer to the SpecFlow wiki on GitHub.
When using MSTest you must also add the Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll to the references of your Ranorex Studio project (“<Microsoft Visual Studio installation folder>Common7IDEPublicAssembliesMicrosoft.VisualStudio.QualityTools.UnitTestFramework.dll”).
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.
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) { ScenarioContext.Current.Pending(); } [When("I press add")] public void WhenIPressAdd() { ScenarioContext.Current.Pending(); } [Then("the result should be (.*) on the screen")] public void ThenTheResultShouldBe(int result) { ScenarioContext.Current.Pending(); } }
SpecFlow searches for the step definition text in the step definition files that matches the text in the scenario step.
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(); ClickNumButton.Start(); } [When("I press the square root button")] public void WhenIPressSqrt() { PressSqrt.Start(); } [Then("the result should be (.*) on the screen")] public void ThenTheResultShouldBe(int result) { ValidateResult.Instance.result = result.ToString(); ValidateResult.Start(); } }
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:
[BeforeScenario] public void BeforeScenario() { StartSUT.Start(); } [AfterScenario] public void AfterScenario() { CloseSUT.Start(); }
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 MSTest. 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.
Run a BDD test using MSTest
Because this solution uses MSTest as the unit test provider, we must use MSTest.exe to execute our tests. Refer to the articles Run automated tests from the command line using MSTest and MSTest.exe command-line options for a detailed description of the MSTest command line tool.
Your command line call should look something like this:
MSTest.exe /testcontainer:<solution>.dll /noisolation
Now the steps defined in the feature file will be executed and the result will be stored in a test result folder. Open the result of your test run for detailed information about the performed steps.
Conclusion
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.
The post How to Use Ranorex Studio in Your BDD Process appeared first on Ranorex.