2014-05-09

Generate dynamic examples for cucumber Scenario outline






In following scenario outline, examples will be generated by `generate_example_data_func`:



Feature: test feature
  Scenario Outline: testing
    * test_tool __________________* "<foo>"
    * test_tool *__________________ "<bar>"
  Examples: 
    | foo                             | bar |
    | `generate_example_data_func`    |     |


implementation of `generate_example_data_func`, and steps:



Given(/^test_tool __________________\* "(.*?)"$/) do |arg1|
end

Given(/^test_tool \*__________________ "(.*?)"$/) do |arg1|
end

def generate_example_data_func table
  table <<  ["xxx", "yyy"]
  table <<  ["aaa", "bbb"]

  table
end

running result:
$ cucumber –expand



Feature: test feature

  Scenario Outline: testing                 # features/reading_report/test.feature:2
    * test_tool __________________* "<foo>" # features/step_definitions/new_step.rb:1
    * test_tool *__________________ "<bar>" # features/step_definitions/new_step.rb:4

    Examples: 

      Scenario: | xxx | yyy |                 # features/reading_report/test.feature:-1
        * test_tool __________________* "xxx"   # features/step_definitions/new_step.rb:1
        * test_tool *__________________ "yyy"   # features/step_definitions/new_step.rb:4

      Scenario: | aaa | bbb |                 # features/reading_report/test.feature:-1
        * test_tool __________________* "aaa"   # features/step_definitions/new_step.rb:1
        * test_tool *__________________ "bbb"   # features/step_definitions/new_step.rb:4

2 scenarios (2 passed)
4 steps (4 passed)
0m0.028s


To support dynamic generate example data, we need change the scenario_outline.rb`:



def create_examples_table(example_section_and_gherkin_examples)
  example_section = example_section_and_gherkin_examples[0]
  gherkin_examples = example_section_and_gherkin_examples[1]

  examples_location    = example_section[0]
  examples_comment     = example_section[1]
  examples_keyword     = example_section[2]
  examples_title       = example_section[3]
  examples_description = example_section[4]
  examples_matrix      = example_section[5]

  #CHANGE BEGIN#####################################################
  #~/.rvm/gems/ruby-1.9.3-p392/gems/cucumber-1.3.14/lib/cucumber/ast/scenario_outline.rb
  if examples_matrix.length > 1 && examples_matrix[1].length > 1
      using_function_to_generate_examples = !(examples_matrix[1][0] =~ /^`.*`$/).nil?

      

      if using_function_to_generate_examples
        function_str = examples_matrix[1][0].gsub!(/`(.*)`/, '\1') + "([" + examples_matrix[0].to_s  + "])"
        examples_matrix = eval(function_str)
      end
  end
  #CHANGE END#####################################################

  examples_table = OutlineTable.new(examples_matrix, self)
  ex = Examples.new(examples_location, examples_comment, examples_keyword, examples_title, examples_description, examples_table)
  ex.gherkin_statement(gherkin_examples)
  ex
end





No comments:

Post a Comment