简体   繁体   English

我可以获取黄瓜每一步的时间戳吗?

[英]Can I get timestamps for each step in cucumber?

In cucumber past, I seem to remember an option to get the time it took for each step to complete 在过去的黄瓜中,我似乎记得有一种选择来获取完成每个步骤所花费的时间

    And I navigate to a widget with widget form
    #features/step_definitions/common_sd.rb:26
  ### STEP COMPLETED   9.634963s

Thinking this functionality went away from cucumber, my work around was to add time stamp functionality to every snippet which feels clunky 认为此功能已脱离黄瓜,我的工作是将时间戳功能添加到每个感觉很笨拙的代码段

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

and

 def step_end(st)
   puts "### STEP COMPLETED   #{(Time.now - st)}s"
 end

I could use the env.rb if I wanted universal hooks for the beginning and end of each scenario, but not steps as far as I can tell. 如果我想在每种情况的开始和结束时都使用通用钩子,则可以使用env.rb ,但就我所知,不应该使用任何步骤。

I can craft some sort of global step wrapper that adds the time metrics and calls each step. 我可以设计某种全局步骤包装器,以添加时间指标并调用每个步骤。

Any ideas on the most elegant way to get time to complete for a step? 您有什么想法可以以最优雅的方式完成工作吗?

This is copy-paste from my features/support/env.rb . 这是从我的features / support / env.rb复制粘贴。 I quit using cucumber years ago and hence I can't provide a sophisticated answer, since I have almost everything forgotten, but I hope this snippet might led you towards right direction: 几年前我退出使用黄瓜,因此我无法提供一个复杂的答案,因为我几乎忘记了所有事情,但是我希望这段代码可以引导您朝正确的方向前进:

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在步骤定义中访问Cucumber步骤名称? - How can I access a Cucumber step name in the step definition? 为什么我在Cucumber中获得&#39;Undefined dynamic step&#39;? - Why do I get 'Undefined dynamic step' in Cucumber? 如何在“黄瓜”步骤中从Rack返回响应? - How do I get the response returned from Rack in a Cucumber step? 如何从黄瓜步骤访问语言字符串? - How can I access language strings from a cucumber step? Cucumber功能可以将常量传递给步骤定义吗? - Can a Cucumber feature pass a constant to a step definition? 如何在黄瓜的步骤定义中添加I18n功能? - How can I add I18n capabilities to my step definitions in cucumber? 我如何找出我刚刚在Cucumber的AfterStep挂钩中执行了哪个步骤? - How can I figure out which step I've just executed in Cucumber's AfterStep hook? 黄瓜步骤定义为“鉴于我已登录” - Cucumber step definition for “Given that I'm logged in” 如何从使用 Capybara 的 Cucumber Step 获取整个页面的响应 HTML? - How do i get the response HTML of an entire Page from a Cucumber Step that uses Capybara? 如何在Ruby中的多个项目之间共享一些常见的页面对象和黄瓜步骤定义 - How can i share some common page objects and cucumber step definitions between multiple projects in Ruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM