Calling Block multiple times in Cucumber Around Hook (Ruby)

前端 未结 2 1214
眼角桃花
眼角桃花 2021-01-14 20:24

I\'m trying to run a scenario several (30) times in order to get a nice statistical sample. However the block is only executing once; each subsequent time results in the sce

相关标签:
2条回答
  • 2021-01-14 20:25

    Currently Cucumber does not seem to support calling the block twice in an around hook. This can be demonstrated by the following feature file:

    Feature: This scenario will print a line
    
      Scenario: Print a line
        When I print a line
    

    And step definitions:

    Around do |scenario, block|
      Kernel.puts "START AROUND, status=#{scenario.status}"
      block.call
      Kernel.puts "BETWEEN CALLS, status=#{scenario.status}"
      block.call
      Kernel.puts "END AROUND, status=#{scenario.status}"
    end
    
    When /^I print a line$/ do
      Kernel.puts "IN THE STEP DEFINITION"
    end
    

    When this is executed, Cucumber will print:

      Scenario: Print line  # features/test1.feature:3
    START AROUND, status=skipped
    IN THE STEP DEFINITION
        When I print a line # features/test.rb:9
    BETWEEN CALLS, status=passed
        When I print a line # features/test.rb:9
    END AROUND, status=passed
    

    Evidently since the status of the scenario is already "passed", Cucumber does not re-execute it, though the output formatter receives the steps. I have not found any way to "reset" the status in the scenario API to get them to be re-run.

    There are other problems with around hooks as well, for example you cannot set variables to the World in around hooks (like you can in before hooks). See also Cucumber issues 52 and 116 for more gory details.

    0 讨论(0)
  • One possibility might be to keep the passed-in block as it is, and call the ".call" method on a duplicate? Something like (untested):

    Around do |scenario, block|
      30.times do
        duplicate = block.dup
        before_hook(scenario)
        duplicate.call
        after_hook(scenario)
      end
    end
    

    Just make sure not to use ".clone" on the block, since clone will create an object with the same Id, resulting in every change made to the duplicate also affecting the original.

    0 讨论(0)
提交回复
热议问题