简体   繁体   English

如何使ruby方法在每个[给定,何时,然后]步骤之前和之后运行?

[英]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.

相关问题 在Ruby中定义方法后如何运行计算? - How to run calculation after methods are defined in Ruby? Ruby方法:如果给出的参数不足,如何返回用法字符串 - Ruby Methods: how to return an usage string when insufficient arguments are given 如何使用Ruby在将每个字符打印成字符串之前进行延迟 - How to make delay before each character is printed in a string using Ruby Ruby Minitest框架的Before / After方法 - Before/After methods for Ruby Minitest framework 依次运行红宝石方法 - Run ruby methods one after the other 在ruby中的“ raise”语句之后,如何使脚本继续运行? - How to make script continues to run after 'raise' statement in ruby? 在Ruby中,如何在层次结构中的特定级别运行对象的每个方法(而不是它继承的方法)? - In Ruby how to run each method of an object at a particular level in the hierarchy (not the methods it inherits)? 是否可以在Ruby中的每一行之后运行代码? - Is it possible to run code after each line in Ruby? 在ruby中,如何将原始源代码加载到Proc的实例?(在类方法中将`before`之后`钩子添加) - In ruby, how to load raw source code to an instance of Proc?(add `before` `after` hook to class methods) 在每个功能之前和之后是否有一个黄瓜钩可以运行 - Is there a cucumber hook to run before and after each feature
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM