测试 龟速的测试, 求解救

42thcoder · November 27, 2014 · Last by CocaColaCat replied at November 30, 2014 · 2814 hits

打开终端,此时 spring 没有在跑. 跑了下测试,大概花了一分钟。ps: 31 个 example 都是些很简单的 demo 测试,完全搞不懂为啥耗时这么久。

~/work/demo ‹ruby-2.1.2›  ‹develop*$ spring status
Spring is not running.

~/work/demo ‹ruby-2.1.2›  ‹develop*$ spring rspec

......具体测试省略

Finished in 41.14 seconds (files took 13.86 seconds to load)
31 examples, 6 failures, 4 pending

这时候再查看下 spring 的状态,发现已经跑起来了。按文档写得,下一次测试应该秒刷, 可耗时居然更久了。

~/work/demo ‹ruby-2.1.2›  ‹develop*$ spring status
Spring is running:

28325 spring server | demo | started 1 min ago
28326 spring app    | demo | started 1 min ago | test mode


~/work/demo ‹ruby-2.1.2›  ‹develop*$ spring rspec

......具体测试省略

Finished in 34.89 seconds (files took 1 minute 47.34 seconds to load)
31 examples, 6 failures, 4 pending

求大神解救~

是不是 feature specs, 用了 capybara cucumber 之类的?

#1 楼 @serco 没啊,我只写了 model 的测试, factory_girls 倒是用了很多

代码贡献出来,让大家帮你看~你贴两个图不知道具体什么情况~

parallel_tests 这个可以一定程度帮助你

尽量不要用 factory_girl 来创建 model 对象,尝试使用 rail 自带的创建: 比如 create(:hotel) 改成 Hotel.create(attributes_for(:hotel)) (spec_helper 中已添加 config.include FactoryGirl::Syntax::Methods)

有使用 database_cleaner 吗?strategy 应该设置为 transaction 而不是 truncation. 不过 30 多个测试就要 40 秒,确实匪夷所思。

#7 楼 @CocaColaCat 用了。按您给的建议,修改了strategy, 耗时很正常了。

第一次跑 spring rspec, 耗时情况:

spring rspec  7.72s user 2.81s system 38% cpu 27.257 total

第二次跑spring rspec, 基本上能做到秒刷,耗时:

spring rspec  0.66s user 0.15s system 8% cpu 9.009 total

太谢谢啦~

#8 楼 @42thcoder 我最近项目也吃过这个配置的亏。😃 可以研究一下不同的配置效果和用况。 difference-between-truncation-transaction-and-deletion-database-strategies 这个 blog 对于选择测试框架提供了全面的角度,值得一看。 7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails

42thcoder closed this topic. 07 May 16:16
You need to Sign in before reply, if you don't have an account, please Sign up first.