簡體   English   中英

我可以獲取黃瓜每一步的時間戳嗎?

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

在過去的黃瓜中,我似乎記得有一種選擇來獲取完成每個步驟所花費的時間

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

認為此功能已脫離黃瓜,我的工作是將時間戳功能添加到每個感覺很笨拙的代碼段

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

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

如果我想在每種情況的開始和結束時都使用通用鈎子,則可以使用env.rb ,但就我所知,不應該使用任何步驟。

我可以設計某種全局步驟包裝器,以添加時間指標並調用每個步驟。

您有什么想法可以以最優雅的方式完成工作嗎?

這是從我的features / support / env.rb復制粘貼。 幾年前我退出使用黃瓜,因此我無法提供一個復雜的答案,因為我幾乎忘記了所有事情,但是我希望這段代碼可以引導您朝正確的方向前進:

# 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM