Ankur Goel

On hunt of awesomeness!

Cuke Up With Cucumber

Recently, I wrote few features in Cucumber. Cucumber is a powerful tool which enables us to write automated tests in functional descriptions. These descriptions are as easy to comprehend, as plain English. The purpose of this tool is to perfom BDD(Behavior-Driven-Development).

Consider this small snippet from my pacf feature:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Feature: PACF

  As a statistician
  So that I can quickly evaluate partial autocorrelation of a series
  I want to evaluate pacf

Background: a timeseries

  Given the following values in a timeseries:
    | timeseries |
    | 10  20  30  40  50  60  70  80  90  100 |
    | 110 120 130 140 150 160 170 180 190 200 |

Scenario: check pacf for 10 lags with unbiased
  When I provide 10 lags for pacf
  When I provide yw yule walker as method
  Then I should get Array as resultant output
  Then I should get 11 values in resultant pacf

Scenario: check pacf for 5 lags with mle
  When I provide 5 lags for pacf
  When I provide mle yule walker as method
  Then I should get Array as resultant output
  Then I should get 6 values in resultant pacf

Yes, these are tests! And they perform the operations as they say.

  • Feature denotes the feature this test will cover. It is followed by the description of the feature as:

    • As a statistician -> (use-case)
    • So that I can quickly evaluate pacf of a seires -> (purpose)
    • I want to evaluate pacf -> (expected result)
  • Given is analogous to before in RSpec. In context of Background, it denotes before all. That is, the forementioned time-series will be available in all scenarios furhter. This timeseries is resolved by Gherkin parser. This is further resolved after parsing by following definition:

1
2
3
4
5
6
7
Given /^the following values in a timeseries:$/ do |series|
  arr = []
  series.hashes.each do |sequence|
    arr += sequence['timeseries'].split(' ').map(&:to_i).to_ts
  end
  @timeseries = arr.to_ts
end
  • Scenarios cover the test cases with the combination of When, And, Then keywords. They are regular English sentences and combine to form a gramatically sound process. These sentences are then captured by regular-expressions written by programmer. For example;
Scenario’s When clause
1
  When I provide 10 lags for pacf
Converted DSL
1
2
3
When /^I provide (\d+) lags for p?acf$/ do |lags|
  @lags = lags.to_i
end

Above will capture the lags and the strings like:

  • When I provide 5 lags for pacf
  • When I provide 10 lags for acf

Result: Compliant for both acf and pacf. :)

You can check my features and step definitions here.

Cheers
Ankur Goel