显示每个 rspec 示例的运行时

问题描述 投票:0回答:2

目前我正在运行超过 1000 个示例,并且需要很长时间才能完成(超过 20 分钟!!!)。

我想确定哪些示例需要更多时间才能完成,是否有任何方法可以运行 rspec 并返回每个示例(单独)完成所需的时间?我正在使用 rspec 1.3.0rspec-rails 1.2.3

ruby-on-rails tdd rspec rspec-rails
2个回答
58
投票

您可以使用分析来列出 10 个最慢的示例:

spec -p spec/*/*_spec.rb --colour --format profile

如果您在特定的测试套件上运行此示例,您可以从较小的示例子集中获取 10 个最慢的示例:

spec -p spec/models/user_spec.rb --format profile

Rspec 的新版本

对于较新版本的 rspec,您应该使用

--profile
选项:

rspec spec/ --profile         | (shows the 10 slowest examples)
rspec spec/ --profile 2       | (shows the 2  slowest examples)

0
投票

将此行添加到

rails_helper.rb
文件

RSpec.configure do |config|
  config.profile_examples = true
end

现在运行

rspec
给出一些像这样的输出:

(times of each test ^^, then)
Finished in 2 minutes 25.1 seconds (files took 1.22 seconds to load)
71 examples, 0 failures, 15 pending
© www.soinside.com 2019 - 2024. All rights reserved.