Over a million developers have joined DZone.

Fire Up BDD Inside Docker With Cucumber and BDDfire

BDDfire allows you to set up the entire framework with code quality, browser testing, cloud testing, API testing, and Docker integration by running three simple commands.

· DevOps Zone

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

The key benefits of behavior-driven development (BDD) practices are communication enhancement and customer satisfaction. You can read more on that from Dan North and Gojko Adzic. Perhaps the biggest practical challenge that stands in the way of reaping those benefits is the burden of provisioning, installation, maintenance of requisite complex and fussy infrastructure and setting up reliable test infrastructure.

Cucumber is one of the most popular BDD frameworks available in the market to achieve the benefits from BDD practices. The problem is that software teams seems to spend a lot of time setting up Cucumber and the necessary toolkit around it. The management has to decide the tools and engineers have to research all the available tools to support agile practices like CI, code quality, test reporting, parallel execution, DevOps integration, etc. The process of getting BDD framework in a mature state becomes lengthy. Obviously, such an approach would be time-consuming, inefficient, and costly. In this post, let's see how to get up and running with three simple commands to set up a Ruby-Cucumber BDD framework with Docker support using BDDfire.

BDDfire

BDDfire is an instant Ruby-Cucumber BDD framework that collectively installs BDD-supporting tools and gets you up and running within a minute. It is a Ruby library that creates a skeleton for the Ruby-Cucumber BDD framework. BDDfire will create all the directories and required files to support the latest open-source libraries in the Ruby-Cucumber framework. You can read about BDDfire here.

BDDfire Features

Cucumber is a very popular BDD framework. Cucumber becomes more effective when it’s plugged with other open-source libraries. Sometimes, we miss most of the useful libraries to be used with Ruby-Cucumber. BDDfire supports lots of modern open-source libraries so that we won’t miss new trends in the world of Ruby-Cucumber.

BDDfire has following features:

  • Create template directories and files for the Cucumber project.
  • Capybara and WebDriver Integration for executing acceptance scenarios in real browsers.
  • Capybara and Poltergeist integration to execute scenarios in headless browsers.
  • Lots of pre-implemented steps so that you don't have to write much code.
  • Supports API testing using with predefined steps.
  • Supports test execution inside Docker containers with PhantomJS.
  • Supports cuke_sniffer and Rubocop libraries which detect smell in the Ruby-Cucumber framework.
  • Supports Relish for the living documentation.
  • Rakefile has been automatically created as a part of BDDfire.
  • Supports YARD documentation of the cucumber project.
  • Supports page object pattern.
  • Load and accessibility testing with Gatling and Axe Engine.

BDDfire Motivation

BDDfire is motivated by few simple facts which could save organisation spending lot time and money on setting up frameworks to enable BDD.

  • Three commands to get up and running with BDD.
  • Write your tests, not framework.
  • Automate with less code.
  • Set up now, learn later.

Three Commands Toward BDD

With BDDfire, we can go from zero to complete BDD framework by running just three simple commands:

$ gem install bddfire 
$ bddfire fire_cucumber 
$ cd cucumber && bundle install

Let's see how to get up and running with the Cucumber-style BDD framework within a few minutes.

Requirements

In order to get started with BDDfire, we have to make sure that following packages are installed on the machine.

  • Ruby >2.0.
  • Docker installed on the machine to run our BDD scenario inside containers.
  • PhantomJS to run scenarios using Poltergeist Capybara driver. On a Mac, you can simply install it using Homebrew brew install phantomjs   
  • Gecko driver to run scenarios inside Firefox. On a Mac, you can simply install it using Homebrew brew install geckodriver. 

BDDfire Installation

BDDfire is a RubyGem and it can be installed using:

$ gem install bddfire

If you are using Ruby, then you have to run the command using sudo. It will take a bit of time to download all the required Ruby libraries. Once finished without errors, you should be able to use the bddfire command. Now, run the bddfire command from the terminal to see all available options.

$ bddfire 

Usage: bddfire <command-name> [parameters] [options]

  <command-name> can be one of
    help
      prints more detailed help information.
    fire_cucumber
      generate a features folder structure.
    fire_accessibility
        setup accessibility checks with axe-matchers
    fire_load
        setup load and performance tests using Gatling
    version
      prints the gem version

That's it. We have successfully installed BDDfire. Now, let's install the Ruby-Cucumber BDD framework using the fire_cucumber command.

$ bddfire fire_cucumber

This will create the cucumber directory and set up all of the frameworks within that directory. BDDfire will create all the required files for our framework.

bddfire_fire_cucumber

Let's cd into the Cucumber directory and install the bundler.

$ cd cucumber
$ bundle install

Congratulations! You have just set up the BDD framework with all the basic toolkits to cover code quality, browser and cloud tests, API tests, and Docker integration. Let's explore how to use it.

Start Running Scenarios

Now that we have installed our Ruby-Cucumber framework with all files and directories, we can run our first headless scenario using Poltergeist.

$ bundle exec rake poltergeist

You won't see that the browser as a scenario is executing within headless browser PhantomJS. However, we can take screenshots and debug information by tweaking Poltergeist setup in the features/support/env.rb  file.

Real and Headless Browser Support

Automated Cucumber scenarios can be run with a real browser or with a headless browser. BDDfire gives you the option to run them in both. Selenium WebDriver can be used to run automated Cucumber scenarios in real browsers without any additional setup. Selenium driver for Capybara is setup for you with a default Cucumber profile. We can run our scenarios in the Firefox or Chrome browser like this:

$ bundle exec rake firefox
$ bundle exec rake chrome

In a similar way, we can run the scenarios in headless browsers like PhantomJS using Poltergeist as the Capybara driver. The Poltergeist driver is already setup for you in the features/support/env.rb file. BDDfire also created a profile to run scenarios with the headless browser. You can see your scenarios executed but you won’t see any browser launched. This helps run automated Cucumber scenarios faster than with real browsers.

There is one more pre-defined scenario for running API Test which checks JSON response from Github. We can run that test using:

$ bundle exec rake api

phantomjs

Cloud Testing Support

BDDfire gives you the option to run tests in third-party cloud testing services like SauceLabs, BrowserStack, and TestingBot. In order to use those services, you need to create an account and get an username and assess key. There are various Rake tasks to the run tests in the Cloud Testing services like SauceLabs, Testing Or BrowserStack. We just need to plug our username and API key in the feature/support/env.rb file.

You can then use that access key in the features/support/env.rb file to run the automated Cucumber scenarios. If you are running tests in BrowserStack, you will have all the stacks defined in the browser.json file. We can use any of them. We need to configure the BrowserStack username and access key in the driver configuration.

$ BS_STACK=osx_firefox bundle exec rake browserstack

Using the same setup, you can run automated scenarios in the other third-party testing services like SauceLabs and TestingBot.

Living Documentation

BDDfire gives you the option to use Relish as a living documentation tool. Relish publishes feature files online so that the whole team can read and access them. In order to use Relish, you need to have a Relish account and API token that can be obtained by visiting this link after logging in.

BDDfire creates a file .relish where you need to paste your token. Now, you can publish your feature files online by using:

$ bundle exec relish push {Publisher}/{project}

Code Quality

BDDfire comes with two code quality checking tools: Rubocop to check Ruby code quality and Cuke_Sniffer to detect smells in the Cucumber features, step definitions, and hooks. BDDfire creates a  .rubocop.yml file with basic Ruby code quality rules. You can change the configuration according to your requirements. With this configuration, you can use Rubocop to detect smell in our Ruby code

$ bundle exec rake rubocop

In a similar way, you can use Cuke_Sniffer to detect smells in the Cucumber project. Cuke_Sniffer runs through all feature files, step definitions, hooks, and support code. It finds out dead steps and suggests improvement for writing better feature files. You can execute Cuke_Sniffer like this:

$ bundle exec rake cake_sniffer

Continuous Integration

BDDfire creates a ci_script  file to run automated Cucumber scenarios on a Continuous Integration server like Hudson and Jenkins. It clears the workspace by deleting old reports, creates YARD documentation for your project, and runs Rubocop and Cuke_Sniffer before executing your scenarios. You can also change this file to suit your project need. You can execute this bash script on any CI by passing one of the rake tasks.

$ ./script/ci_script RAKE-TASK

Add a Scenario With Pre-Defined Steps

BDDfire allows us to use predefined steps so that we don't have to write more code while writing Cucumber steps. BDDfire has following predefined steps which can be used anytime during your project. This is a major advantage if the test engineer is non-technical. He or she can write automation code using those steps.

Browser Based Pre-Defined Steps

You can use browser-based steps to run scenarios within browsers like Google Chome or Firefox.

Poltergeist Based Pre-Defined Steps

You can use headless Poltergeist-based steps to run BDD scenarios with PhantomJS, which is a headless browser.

API Based Pre-Defined Steps

Just tag your scenario or feature with @api and you can use inbuilt predefined API-based steps.

Start Running Scenarios Within Docker Containers

Docker has become a powerful tool in the world of Continuous Delivery and continuous deployment. Software teams have to provision all the infrastructure to run as a Docker container. It would be awesome if we could able to run Cucumber scenarios inside the Docker container. Fortunately, BDDfire has done all that provisioning work for us; however, we can only run PhantonJS- and Poltergeist-based headless scenarios within Docker Containers. BDDfire will:

  • Build a Docker image with provisioned with Ruby and PhantomJS.
  • Map the project directory to Docker container and install software.
  • Run BDD scenarios inside the Docker container.

Assuming that you have Docker installed, you just need to need to execute this Docker rake task:

$ bundle exec rake docker

This will take a while, as Docker images will be built from scratch. Be patient and watch the BDD scenarios running inside containers at the end.

docker

We can mount the volume of our project repository and docker containers to generate and share test reports. You can modify Dockerfile  and script docker.sh  according to your needs. In this way, we can run our BDD scenarios inside Docker containers.

BDDfire will also support non-functional testing like load and performance testing with Gatling and accessibility testing using Axe engine which we will cover separate article.

Conclusion

We have seen that the process of setting up a BDD test framework be very quick using tools like BDDfire. We can set up the entire framework with code quality, browser testing, cloud testing, API testing, and Docker integration by running three simple commands. Now, we can spend more time writing BDD scenarios instead of writing the framework.

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:
docker ,devops ,bddfire ,behavior driven development ,cucumber ,ruby

Published at DZone with permission of Shashikant Jagtap. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}