【问题标题】:Print duration of every step in terminal, during watir-cucumber execution在 witir-cucumber 执行期间打印终端中每个步骤的持续时间
【发布时间】:2014-12-11 09:05:41
【问题描述】:

在 watir-cucumber 执行期间,是否有一种标准方法可以打印终端中每个步骤的持续时间?

使用“后续步骤”和全局变量可能会做出某种发明。但是,有没有合适的方法?

【问题讨论】:

    标签: cucumber watir scenarios


    【解决方案1】:

    你可以像这样使用钩子:

    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

    【讨论】:

      【解决方案2】:

      最简单的方法是使用 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
      

      【讨论】:

      • 大项目,你有数百个step_definitions。如果您想要特定功能的信息,但不是全部,这种方法很好。
      • 如果代码包含在“后续步骤”中,这样​​的事情可能会起作用。
      猜你喜欢
      • 1970-01-01
      • 2019-04-24
      • 2018-07-16
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多