调用块多次在Cucumber Hook(Ruby) [英] Calling Block multiple times in Cucumber Around Hook (Ruby)

查看:410
本文介绍了调用块多次在Cucumber Hook(Ruby)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个场景几(30)次,以获得一个不错的统计样本。但是块只执行一次;每个随后的时间导致场景被调用并且不执行(尽管它表示该场景成功地完成了约5ms的时间)。

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 scenario being called and not executing (although it says that the scenario did successfully complete with a time of around 5 ms).

Around('@mass_benchmark') do |scenario, block|
  $seconds_taken = "SECONDS TAKEN NOT SET"
  @time_array = []
  30.times do
    before_hook(scenario)
    block.call
    after_hook(scenario)
    @time_array << $seconds_taken
  end
  write_time_array_to_file(@time_array, scenario_name)
end

标记@mass_benchmark执行此块,而不是〜@ mass_benchmark,它只是正常执行场景。方法before_hook和after_hook复制Before('〜@ mass_benchmark')和After('〜@ mass_benchmark')钩子(实际上只是调用相同的方法)。

The tag @mass_benchmark executes this block, as opposed to ~@mass_benchmark, which just executes the scenario normally. The methods before_hook and after_hook replicate the Before ('~@mass_benchmark') and After ('~@mass_benchmark') hooks (which actually just call the same method).

变量$ seconds_taken设置在我正在计时的特定区域周围。我不是在整个测试的时间,只是它的一个关键部分;测试的其余部分到达那个点,等等,这不是定时部分的一部分,所以我不能只是移动时间部分在这之外。

The variable $seconds_taken is set around the specific area for which I am timing. I am not timing the whole test there, just a critical portion of it; the remainder of the test is getting to that point, etc, which is not to be part of the timed portion, so I cannot just move the timing portion outside of this.

问题可能是我在这些方法中做的,但就我所知,一切正常(如良好的put语句所示)。任何想法赞赏!

The issue may be with something I'm doing in those methods, but as far as I can tell, everything works normally (as indicated by well-placed puts statements). Any ideas are appreciated!

推荐答案

目前Cucumber似乎不支持调用块两次在一个挂钩。这可以通过以下功能文件来证明:

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

而步定义:

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

将打印:

  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

显然,由于场景的状态已经是传递,Cucumber不会重新执行它,虽然输出格式化器接收步骤。我没有找到任何方法来重置场景API中的状态,让他们重新运行。

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.

还有其他问题,例如你不能在环绕钩子中设置变量到世界上(就像你可以在钩子之前一样)。另请参见黄瓜问题 52 116 了解更多详情。

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.

这篇关于调用块多次在Cucumber Hook(Ruby)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆