[英]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. Ruby带有一个基准测试模块,可以很轻松地为您处理这些工作。
Edit to address the comments below 编辑以解决以下评论
In Cucumber, there is a hook called Around
that allows you to wrap your scenario however you wish. 在Cucumber中,有一个名为
Around
的钩子,可让您根据需要包装方案。 For example, straight from the docs: 例如,直接来自文档:
Around('@fast') do |scenario, block|
Timeout.timeout(0.5) do
block.call #actually runs the scenario
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.