【问题标题】:how to find the time taken for each testcase in Rspec如何在Rspec中找到每个测试用例所花费的时间
【发布时间】:2018-12-03 06:56:16
【问题描述】:

我在我的项目中使用 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 必须提供一些预定义的方法来返回每个测试用例的持续时间,你知道吗?

【问题讨论】:

  • rspec 可以打印 10 个最慢的规范及其执行时间。这对你来说足够好吗?或者您需要 所有 规范?
  • 数字 10 可通过 profile_examples 设置进行配置。因此,如果您只需要查看所有时间,您就可以将其设置为比测试次数多的数字 - relishapp.com/rspec/rspec-core/docs/configuration/…

标签: ruby rspec capybara watir


【解决方案1】:

RSpec 有一个 example_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

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

示例 ID |状态 |运行时间 | --------------- | ------ | ------------ | my_spec.rb[1:1] |通过 | 3.02 秒 | my_spec.rb[1:2] |通过 | 2.01 秒 |

【讨论】:

  • 嘿,这对我来说是一个了不起的信息。但是我想要更多信息,我可以更改第一列而不是这个my_spec.rb[1:1] 这个does stuff 和而不是这个my_spec.rb[1:2] 这个does more stuff 吗?我可以吗 ?并且我可以再添加一列作为错误消息的错误吗?
  • @Rajagopalan 听起来你真正想要的只是实现一个自定义格式化程序 - relishapp.com/rspec/rspec-core/v/3-8/docs/formatters/…
  • @Rajagopalan,据我所知,没有可用的配置选项。您可以查看github.com/rspec/rspec-core/blob/master/lib/rspec/core/…的代码
  • 为了扩展@ThomasWalpole 的建议,JSON 格式化程序输出描述和运行时间。您可以使用该格式化程序并解析 JSON 结果,也可以将其用作自定义格式化程序的起点。
  • @ThomasWalpole 非常感谢。我会看看这个。
【解决方案2】:

如果您想要更多细节和/或想要控制格式,您可以创建自定义格式器。

例如,给定以下规格:

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

这给了我们:

有些事做事|失败 | 3.010178 | {:class=>"RuntimeError", :message=>"some error", :backtrace=>["my_spec.rb:21:in `block... (例如截断) 有些事情做更多的事情|通过 | 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

这给出了:

示例、状态、运行时间、异常 有些事情做的东西,失败,3.020176,"{:class=>""RuntimeError"", :message=>""some error"", :backtrace=>[""my_spec.rb:25:in `block.. .(截断的例子)“ 有些事情做更多的事情,通过,2.020113,

【讨论】:

  • 您好,能否请您提供可能的标题和整洁的格式?特别是它看起来好像在您的第一个答案输出中?如果你不愿意,那也没关系。但如果你这样做会非常有帮助。
  • @Rajagopalan,您需要实际输出为文本还是可以使用 CSV 文件?
  • 是的,我可以使用 CSV 文件,这样我就可以在 Excel 文件中打开它。你能帮帮我吗?
【解决方案3】:

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

【讨论】:

  • 没有这个 run_time 方法正在返回 nil 但如果我调用方法 started_at 那么它正在打印 2018-12-03 16:06:42 +0530
  • require 'rspec' describe 'My behaviour' do it 'test' do sleep 10 puts 'Raja' end after do |example| p example.execution_result.run_time end end 你可以运行这个例子并检查一下。它返回 nil。
  • @Rajagopalan 那是因为在after(:each) 内部,测试实际上仍在进行中,完成时间尚未确定。
  • 如果是这样,我可以知道为什么建议我使用这种方法吗?我没有完全理解你,你能解释一下吗?
  • @Rajagopalan 因为您问 RSpec 为测试时间提供了什么方法,那就是方法。您需要使用 pretend/append_after/around 调整您的周围或之后的块顺序以使其最后执行以便设置时间,但是正如我在另一个答案中提到的那样,听起来您真的想要一个自定义格式化程序,该格式化程序将被调用设置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-13
  • 1970-01-01
相关资源
最近更新 更多