代码之家  ›  专栏  ›  技术社区  ›  Rajagopalan

如何在Rspec中找到每个测试用例所花费的时间

  •  0
  • Rajagopalan  · 技术社区  · 6 年前

    我在我的项目中使用Rspec,我想打印每个测试用例所花费的时间,Rspec是否提供了任何预构建的功能?我可以将测试用例的开始时间 example.execution_result.started_at 但是我不知道如何计算测试用例的结束时间,如果我可以计算结束时间,那么我可以从开始时间中减去结束时间,得到每个测试用例的持续时间。这里有人帮我吗?我已经写了这个代码

    around(:each) do |example|
      startTime=Time.now
      var=example.run
      puts var
      endTime=Time.now
      duration=endTime-startTime
      puts "Time Taken->#{duration.to_f/60.to_f}"
    end
    

    但是我坚信Rspec必须给出一些预定义的方法来返回每个测试用例的持续时间,你知道吗?

    3 回复  |  直到 6 年前
        1
  •  4
  •   Justin Ko    6 年前

    RSpec有一个示例_status _persistence _file _path配置,该配置生成一个文件,其中包含每个单独测试的运行时间。

    例如,给定以下RSpec配置/示例:

    require 'rspec/autorun'
    
    # Enable the reporting
    RSpec.configure do |c|
      c.example_status_persistence_file_path  = 'some_file.txt'
    end
    
    # Run some tests
    RSpec.describe 'some thing' do
      it 'does stuff' do
        sleep(3)
      end
    
      it 'does more stuff' do
        sleep(2)
      end
    end
    

    生成每个示例的状态和运行时报告:

    example_id      | status | run_time     |
    --------------- | ------ | ------------ |
    my_spec.rb[1:1] | passed | 3.02 seconds |
    my_spec.rb[1:2] | passed | 2.01 seconds |
    
        2
  •  3
  •   Justin Ko    6 年前

    如果需要更多详细信息和/或控制格式,可以创建自定义格式设置程序。

    例如,给定以下规格:

    RSpec.describe 'some thing' do
      it 'does stuff' do
        sleep(3)
        raise('some error')
      end
    
      it 'does more stuff' do
        sleep(2)
      end
    end
    

    输出-文本

    我们可以添加自定义格式化程序来输出完整的测试描述、状态、运行时和异常:

    class ExampleFormatter < RSpec::Core::Formatters::JsonFormatter
      RSpec::Core::Formatters.register self
    
      def close(_notification)
        @output_hash[:examples].map do |ex|
          output.puts [ex[:full_description], ex[:status], ex[:run_time], ex[:exception]].join(' | ')
        end
      end
    end
    
    RSpec.configure do |c|
      c.formatter = ExampleFormatter
    end
    

    some thing does stuff | failed | 3.010178 | {:class=>"RuntimeError", :message=>"some error", :backtrace=>["my_spec.rb:21:in `block... (truncated for example)
    some thing does more stuff | passed | 2.019578 | 
    

    输出-CSV

    格式化程序可以修改为输出到CSV:

    require 'csv'
    
    class ExampleFormatter < RSpec::Core::Formatters::JsonFormatter
      RSpec::Core::Formatters.register self
    
      def close(_notification)
        with_headers = {write_headers: true, headers: ['Example', 'Status', 'Run Time', 'Exception']}
        CSV.open(output.path, 'w', with_headers) do |csv|
          @output_hash[:examples].map do |ex|
            csv << [ex[:full_description], ex[:status], ex[:run_time], ex[:exception]]
          end
        end
      end
    end
    
    RSpec.configure do |c|
      c.add_formatter(ExampleFormatter, 'my_spec_log.csv')
    end
    

    Example,Status,Run Time,Exception
    some thing does stuff,failed,3.020176,"{:class=>""RuntimeError"", :message=>""some error"", :backtrace=>[""my_spec.rb:25:in `block...(truncated example)"
    some thing does more stuff,passed,2.020113,
    
        3
  •  2
  •   Thomas Walpole    6 年前

    每个示例都会获得一个ExecutionResult对象,该对象具有 run_time 方法,所以 example.execution_result.run_time 应该满足你的要求