Cucumber data-driven testing tips

Very often I hear testers wondering how to pass data from step to step in Cucumber. I usually store important data in variables or, for instance, store user data in hashes.

Let’s see an example:

Implementation of the first test step:

As you can see I store all user data in hashes. Assumption is that I would not need more than three different users in my test scenarios. You could even store created user data in hash ‘$latest’ which could be needed in some scenarios (latest created user). Now it is easy to use stored data in next steps e.g.

Sometimes we could have problems with creating of users because the data needs to be unique. So second time you run your test it fails since same user is already created. I use keywords in my data tables to created different random strings.

The rules I use are (you can set your own rules):

  • ${} – everything inside will be parsed, strings are comma separated
  •  numerical value – create random alphanumeric string
  •  ! – use the string as it is
  •  N – random numbers
  •  S – random alphanumeric uppercase string
  •  s – random alphanumeric lowercase string

So ${!0046,N8} will produce ‘0046’ followed by 8 random numbers. ${!test,4,@,8,!.com} will generate ‘test’ followed by 4 random alphanumeric letters, symbol ‘@’, 8 random alphanumeric letters and ‘.com’. Implementation could be like this:

‘parse_input’ method is pretty simple to implement so I will not list the code here.

Using such keywords for creating random string gives possibility for testers to do a lot of negative data driven test scenarios using ‘Scenario Outline’ and ‘Examples’ (wrong format, missing values, too large, too small etc.) They could also use such scripts for exploratory test automation – changing values in data tables and running tests.
Observe that it is important to also log your random values to know what values were used if a test fails.

This Post Has 5 Comments

  1. What i have done in the past is to defined a method in my env.rb file
    def test_context
    @test_context ||= Hash.new
    end

    and in my test i can save a value as: test_context[:customer_name] = @browser.span(:id => “name”).text

    and then i can simply call test_context[:customer_name] in other steps to get the value i have previously saved.

    And at the end of every scenario, i clear the test_context by doing:

    After do |scenario|
    @test_context = nil
    end

    1. Thank you Samuel. It was a good suggestion.

  2. How do you set rules for passing unique data in cucumber file? Sorry, am new to cucumber and learning it.

  3. Hi Davor,

    Does this work for java, because when I tried to implement in the below fashion, it still entered as $N8 in the From Account Field.

    And I enter the following transfer details
    | From Account | To Account | TxnAmt | PmtRef | Schedule |
    | ${N8} | 50550561 | ${N6} | Auto Test | now |

    Am I missing anything? Could you pls help

  4. Hi Davor,

    Does this work for Java? Because when I tried the below code it just returned $N8 in my From Account field?

    And I enter the following transfer details
    | From Account | To Account | TxnAmt | PmtRef | Schedule |
    | ${N8} | 50550561 | ${N6} | Auto Test | now |

    Am I missing something here?

Leave a Reply

Close Menu