简体   繁体   中英

How to make ruby methods run before and after each [given, when, then] step?

I'm trying to get some easy-to-read time metrics for how long steps of each scenario take to run.

I have methods that insert rows into a logging table like this:

puts "#{Time.now} - Starting Given"

puts "#{Time.now} - Ending Given"

etc...

And I want them to be called automatically at the start and end of each step, but I can only find ways to run them before or after the entire scenario , rather than each step.

Is there a way to do this?

Ruby comes with a benchmarking module to handle this stuff pretty easily for you.

Edit to address the comments below

In Cucumber, there is a hook called Around that allows you to wrap your scenario however you wish. For example, straight from the docs:

Around('@fast') do |scenario, block|
  Timeout.timeout(0.5) do
    block.call #actually runs the scenario
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM