BDD

BDD Code analysis with Cucumber and Cuke_Sniffer

BDD Code

Behavior Driven Development a.k.a BDD is a recent and modern software development agile framework. While writing software using BDD, we use different tools but code is structured in similar way. It consist of following

  • Feature Files

Feature files are the set of features/stories and scenarios in it. Feature files are generally written in the business readable Domain Specific Language [DSL] called “Gherkin“. Gherkin use ‘Given When Then’ a.k.a GWT format for the scenarios. The feature file has .feature extension. Gherkin can be understood by most of the BDD tools. The feature file may look like this

  • Step Definitions

The step definitions is the code which glue the gap between business logic and technical aspect. This code makes Gherkin feature work. The step definitions can be written in the different language. The example for the step definition file with Cucumber and Ruby for the above feature file.

  • Support Code

The support code consist of configuration of the environment and driver  (selenium, phantomjs etc ) for the web acceptance framework. The example of the support code file for the cucumber (env.rb)

  • Hooks

Hook is the piece of code that can be run before or after each scenario, feature, suite or step. There are different types of hook. [hook.rb]

  • Config

The config file can be used to set profile with which we can run our features. This file can also used to generate reports. [cucumber.yml

  • Dependencies

There are different dependency management system for different programming languages. e.g Composer for PHP, bundler for Ruby.

We have example ‘Gemfile’ where we declare all our dependencies.

Report

This may be optional but you can create a directory for storing html reports and other reports like cuke_sniffer reports. On CI boxes, you can create and delete these directories temporarily.

BDD code structure for me looks like this in my Sublime Text

BDD_code

 

 BDD Code Analysis with Cuke Sniffer

  Cuke_sniffer is the ruby gem and can be used for the code analysis for any cucumber project.

Installation

In order to use Cuke_Sniffer’ you need to have ruby version above 1.9.3.

Or Simply add this to your projects ‘Gemfile’

Now update the bundle

You are done  and ready to use cuke_sniffer.

Usage

Now navigate to your project where features directory is located and run

When I run it with demo project, I got

cuke_sniffer

We can generate Cuke_sniffer html reports like this

This will generate html report and for the code analysis.

cuke_sniffer_html

Benefits of Cuke_sniffer

  • Cuke_sniffer analyses all feature file, step definitions and hooks for improvements
  • Cuke_sniffer tells us about any dead steps
  • Cuke_sniffer detect smells in the feature files and step definitions
  • Cuke_sniffer detect how many times hooks are used

GitHub

Source code of this demo is available on the GitHub

Cuke_sniffer_demo

HTML Report can be found here.

Conclusion

Cuke_sniffer can be used in the any Cucumber project for better BDD code and improve code continuously.