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

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

问题描述

我正在尝试运行一个场景数 (30) 次,以获得一个不错的统计样本.但是该块只执行一次;每次后续时间都会导致场景被调用而不是执行(尽管它说场景确实在大约 5 毫秒的时间内成功完成).

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.

问题可能与我在这些方法中所做的事情有关,但据我所知,一切正常(如放置良好的 puts 语句所示).任何想法表示赞赏!

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 似乎不支持在 around hook 中调用 block 两次.这可以通过以下功能文件来证明:

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

执行此操作时,Cucumber 将打印:

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

显然,由于场景的状态已经通过",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.

环绕钩子还有其他问题,例如你不能在环绕钩子中为世界设置变量(就像你可以在前钩子中一样).另请参阅黄瓜问题 52116 了解更多血腥细节.

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 Around Hook (Ruby) 中多次调用 Block的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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