Cucumber, Ruby and Rake

Export features and scenarios for test automation with Cucumber when using Rake as your build tool

Alan Parkinson avatar
Written by Alan Parkinson
Updated over a week ago

You have two options for working Behave Pro, Cucumber and Ruby: 

  1. The command line client

  2. Rake build scripts Both require the Behave gem to be installed

Installing the Behave gem

The gem is hosted on RubyGems at https://rubygems.org/gems/behave and is available direct from the website or using RubyGems. Assuming you have Ruby and RubyGems installed on your system, run:

$ gem install behave

and you will have installed the behave ruby client and its dependencies. Depending on your system, you may have to prefix the command with “sudo”.

Using the command line

Installing the gem will have placed a binary on your path named “behave”. The output of “behave –help” is as follows:

Usage: behave [options]

Specific options:
    -h, --host HOST                  Host URI for Jira installation - This should be https://behave.pro OR Jira Server URL
    -u, --user USER                  userId OR Jira username
    -p, --pass PASS                  API key OR Jira Password
    -k, --key KEY                    Project ID from the Jira Project Admin Page
    -d, --directory [DIR]            Specify output directory (default 'features')
    -m, --manual                     Include manual tagged scenarios in download
        --proxy PROXY_URL            Pass requests through a PROXY_URL
        --bypass-ssl                 Bypass SSL certificate checking
        --help                       Show this message
        --version                    Show version

An example call could look like this:

$ behave --host https://behave.pro \
  --user "userId OR Jira Server Username" \
  --pass "API key OR Jira Server Password" \
  --key 10100 \
  --directory features \
  --manual

Note: The host for Cloud should be as above, but for Jira Server, enter your full Jira Server instance URL.

This would download all .feature files from the 10100 project to the features directory (relative to the directory behave was run from), including all scenarios tagged as manual.

Rake

You are more than likely to be using Rake in your Ruby project for scripting your builds. Cucumber comes with its own Rake task ready for you to use and looks like this:

require 'cucumber/rake/task'

Cucumber::Rake::Task.new(:features) do |t|
  t.cucumber_opts = "--format pretty"
end

This cucumber task can be executed using the following command;

$ rake features

As our Features and Scenarios are stored within Jira/Behave Pro we will need to download them. To do this we will add a new Rake task that will download them using the “Behave” Ruby gem.

require 'behave'

desc "Download features"
task :download do
  Behave::FeatureDownloader.new({
    'host'   => 'https://behave.pro for Cloud OR Jira Server URL',
    'user'   => 'Cloud userId OR Jira Server Username',
    'pass'   => 'Cloud API key OR Jira Server Password',
    'key'    => 10100,
    'dir'    => 'features',
    'manual' => true
  })
end

The task has four required parameters, host , key , user  and pass . These 4 parameters are different depending on if you are using Jira Cloud or Server. You can retrieve the correct details the Jira project's admin area; 'Project Settings' > 'Behave Pro' > 'Generate config'. This article has more information on finding these configuration items

This task can be executed using the following command:

$ rake download

We can now download our Features and Scenarios from Jira/Behave Pro but we will always have to remember to run “rake download” before “rake features”. To avoid this we can configure the Cucumber “features” task to depend on the Behave “download” task and execute it before running Cucumber. This just requires one additional line in your rake file.

task :features => :download

The full Rakefile can be found in the example project on Bitbucket.

Proxy

If you need to specify a proxy to pass requests through, use the –proxy flag from the command line or pass 'proxy’ => 'MY_PROXY’ argument into FeatureDownloader i.e.

$ behave --host https://behave.pro \
  --proxy https://some.proxy.url

or

task :download do
  Behave::FeatureDownloader.new({
    ...
    'proxy' => 'http://some.proxy.url'
    ...
  })
end

Bypassing SSL

If you experience problems with your SSL certificate, you can bypass SSL checking in behave by using the –bypass-ssl command line flag, or the 'bypass_ssl’ => true FeatureDownloader argument.

Did this answer your question?