RandomQuestion
RandomQuestion

Reputation: 6988

How to run same scenario multiple times without using outline in Cucumber?

Consider following cucumber scenario:

Scenario Outline: Execute a template
 Given I have a <template>
 when I execute the template
 Then the result should be successful.
 Examples:
   | template  |
   | templateA |  
   | templateB |  

So this will run above scenario outline with values mentioned in the table. But this requires me to know all the templates I need to execute in advance and fill them in the table. Is there a way to dynamically load list of templates and then execute the scenario for each of those templates?

Advantage of that approach will be, whenever I add a new template, I wouldn't need to update the feature tests.

Thanks

Upvotes: 0

Views: 1980

Answers (1)

diabolist
diabolist

Reputation: 4099

Yes there is, put the list of templates in your step definitions, or even better in your application. Then you can write

  Scenario: Execute all templates
    When I execute all the templates
    Then there should get no errors

The interesting thing is how you implement this. Something like:

module TemplateStepHelper
  def all_templates
    # either list all the templates here, or better still call a method in 
    # the app to get them
    ...
  end
end
World TemplateStepHelper

When "I execute all the templates" do
  @errors = []
  all_templates.each do |t|
    res = t.execute
    @errors << res unless res
  end
end

Then "there should be no errors" do
  expect(@errors).to be_empty
end

The exact details of the code can be modified to meet your needs.

I would recommend moving the code out of the step defs and into methods in the step helper.

Finally if you get the templates from the application, you won't even have to update your features when you add a new template.

Upvotes: 1

Related Questions