Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 我可以得到黄瓜每一步的时间戳吗?_Ruby_Cucumber - Fatal编程技术网

Ruby 我可以得到黄瓜每一步的时间戳吗?

Ruby 我可以得到黄瓜每一步的时间戳吗?,ruby,cucumber,Ruby,Cucumber,在过去,我似乎记得有一个选项可以获得完成每一步所需的时间 And I navigate to a widget with widget form #features/step_definitions/common_sd.rb:26 ### STEP COMPLETED 9.634963s 考虑到cucumber不再使用这个功能,我的工作是在每个感觉笨拙的片段中添加时间戳功能 st = Time.now ... step_end(st) 及 如果我想为每个场景的开

在过去,我似乎记得有一个选项可以获得完成每一步所需的时间

    And I navigate to a widget with widget form
    #features/step_definitions/common_sd.rb:26
  ### STEP COMPLETED   9.634963s
考虑到cucumber不再使用这个功能,我的工作是在每个感觉笨拙的片段中添加时间戳功能

 st = Time.now
 ...
 step_end(st)

如果我想为每个场景的开始和结束使用通用钩子,那么我可以使用
env.rb
,但就我所知,不是步骤

我可以制作某种全局步骤包装器,添加时间度量并调用每个步骤


有什么最优雅的方法来完成一个步骤吗?

这是我的功能/support/env.rb中的复制粘贴。我几年前就停止使用cucumber了,因此我无法提供一个复杂的答案,因为我几乎忘记了一切,但我希望这段代码可能会引导您朝着正确的方向发展:

# encoding: utf-8

require 'bundler/setup'
require 'rspec/expectations'

MAX_SCENARIOS = 10
scenario_times = {}

Around() do |scenario, block|
  start = Time.now
  block.call
  sc = if scenario.respond_to?(:scenario_outline)
         scenario.scenario_outline
       else
         scenario
       end
  t = scenario_times["#{sc.feature.file}::#{scenario.name}"] = Time.now - start
  # puts "### STEP COMPLETED #{t}s"
end

# print top 10 sorted by execution time
at_exit do
  max_scenarios = if scenario_times.size > MAX_SCENARIOS
                    MAX_SCENARIOS
                  else
                    scenario_times.size
                  end

  puts '—'*20 + "  top #{max_scenarios} slowest  " + '—'*20
  sorted_times = scenario_times.sort { |a, b| b[1] <=> a[1] }
  sorted_times[0..max_scenarios - 1].each do |key, value|
    puts "#{value.round(5)}  #{key}"
  end 
end
编码:utf-8 需要“捆绑机/设置” 要求“rspec/预期” 最大值=10 情景_次={} 环绕()执行|场景,块| 开始=时间。现在 阻塞呼叫 sc=假设情景。响应?(:情景概述) 情景 其他的 脚本 结束 t=scenario_times[“#{sc.feature.file}::#{scenario.name}”]=Time.now-start #将“####步骤已完成#{t}s”放入 结束 #打印按执行时间排序的前10名 在你出口吗 max\u scenarios=如果scenario\u乘以.size>max\u scenarios 最大值场景 其他的 场景_乘以大小 结束 将'-'*20+'放在顶部{max_场景}最慢的'+'-'*20 sorted|u times=scenario|u times.sort{a,b | b[1]a[1]} 已排序的|次[0..max|u场景-1]。每个do |键,值| 放置“#{value.round(5)}#{key}” 结束 结束
您是否查看了Cumber提供的现有挂钩?具体来说,是的,但这仍然是每个场景中增加的一行。如果我知道我希望在每一步上都有时间运行,我真的很想编写一次全局命令,或者找到难以捉摸的cumber标志
cumber--timestamp features\tests\
# encoding: utf-8

require 'bundler/setup'
require 'rspec/expectations'

MAX_SCENARIOS = 10
scenario_times = {}

Around() do |scenario, block|
  start = Time.now
  block.call
  sc = if scenario.respond_to?(:scenario_outline)
         scenario.scenario_outline
       else
         scenario
       end
  t = scenario_times["#{sc.feature.file}::#{scenario.name}"] = Time.now - start
  # puts "### STEP COMPLETED #{t}s"
end

# print top 10 sorted by execution time
at_exit do
  max_scenarios = if scenario_times.size > MAX_SCENARIOS
                    MAX_SCENARIOS
                  else
                    scenario_times.size
                  end

  puts '—'*20 + "  top #{max_scenarios} slowest  " + '—'*20
  sorted_times = scenario_times.sort { |a, b| b[1] <=> a[1] }
  sorted_times[0..max_scenarios - 1].each do |key, value|
    puts "#{value.round(5)}  #{key}"
  end 
end