【发布时间】:2014-12-11 09:05:41
【问题描述】:
在 watir-cucumber 执行期间,是否有一种标准方法可以打印终端中每个步骤的持续时间?
使用“后续步骤”和全局变量可能会做出某种发明。但是,有没有合适的方法?
【问题讨论】:
在 watir-cucumber 执行期间,是否有一种标准方法可以打印终端中每个步骤的持续时间?
使用“后续步骤”和全局变量可能会做出某种发明。但是,有没有合适的方法?
【问题讨论】:
你可以像这样使用钩子:
Before do
@last_time = Time.new
end
AfterStep do
now = Time.new
puts "Step duration: #{((now - @last_time)*100).round} ms" # if (now-@last_time) > 10
@last_time = now
end
通过在新行上输出持续时间会更加冗长,但除非您想要更深入地了解(通过创建自己的格式化程序),否则您无法在指定的位置输出内容。
如果您只对耗时超过特定阈值的步骤感兴趣,请取消注释 if。
【讨论】:
最简单的方法是使用 Ruby 内置的 Time 类。使用 Time.new,我们可以在执行步骤定义中的代码之前和之后将当前时间记录到一个局部变量中。然后我们通过从 end_time 中减去 start_time 来计算持续时间。
#get the start time
start_time = Time.new
#do whatever the step needs to do here
#get the end time
end_time = Time. new
#calculate duration
duration = end_time - start_time
puts duration
【讨论】: