Rspec,Cucumber:最好的速度数据库清理策略 [英] Rspec, Cucumber: best speed database clean strategy

查看:161
本文介绍了Rspec,Cucumber:最好的速度数据库清理策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提高测试的速度。


  1. 我应该使用 use_transactional_fixtures 或者使用 database_cleaner gem?

  2. 哪个database_cleaner策略是最好的?我注意到,从:truncation :transaction 迁移之后,我的800多个示例运行大约4倍! li>
  3. 当我使用database_cleaner :transaction ?时,是否应该关闭 use_transactional_fixtures >
  4. 是的,rack_test的最佳策略是:transaction

  5. 当使用selenium或:transaction 到:truncation /github.com/bernerdschaefer/akephalosrel =noreferrer> akephalos

PS Mysql,Rails 3,Rspec2,Cucumber



P.P.S。我知道 spork parallel_test 并使用它们。但是他们偏偏。例如,Spork在整个套件运行中保存大约15-20秒,但从:transaction 更改为:truncation 将运行时间从3.5分钟增加到13.5分钟(差异10分钟)。

解决方案

4。如果您使用capybara的默认引擎rack_test,您应该使用事务( use_transactional_fixtures 或来自database_cleaner gem的事务支持)。正如你所指出的,使用事务比使用截断策略要快得多。然而,当数据库写入可以通过不同的线程(如同selenium)事务将无法工作。所以你需要使用截断(或强制所有东西通过一个数据库线程 - 另一个选项)。



3。是的,您应该在使用database_cleaner gem时关闭 use_transactional_fixtures 宝石本地支持事务。如果你只需要事务,那么只需要使用use_transactional_fixtures,不要加载database_cleaner gem。



5。下面的代码将在:transaction :truncation 。 (用rspec,capybara,rails3测试这个)



功能这应该给你两个世界的最好的。当您不需要测试JavaScript资料和灵活性时, rack_test 速度 > selenium 。



此外,此代码会在需要时重新填充种子数据(此方法假设您使用seeds.rb加载种子数据 - 约定)。



将以下代码添加到spec_helper。

  config .use_transactional_fixtures = false 
RSpec.configure do | config |
config.before(:suite)do
require#{Rails.root} /db/seeds.rb
end

config.before:each do
if Capybara.current_driver ==:rack_test
DatabaseCleaner.strategy =:transaction
else
DatabaseCleaner.strategy =:truncation
end
DatabaseCleaner.start
end
config.after(:each)do
if Capybara.current_driver ==:rack_test
DatabaseCleaner.clean
else
DatabaseCleaner.clean
load#{Rails.root} /db/seeds.rb
end
end
end

感谢

$ b


$ b

上述解决方案假设您已经知道如何在运行中切换驱动程序。如果有些人不来,这里是如何:



如上所述,假设你通常会使用默认的capybara驱动程序rack_test,但需要使用selenium测试一些Ajaxy的东西。当您要使用selenium驱动程序使用:js => true @javascript 。例如:



Rspec示例:

  ,:js => true do 

Cucumber示例:

  @javascript 
场景:做某事Ajaxy


I would like to increase the speed of my tests.

  1. Should I use use_transactional_fixtures or go with the database_cleaner gem?
  2. Which database_cleaner strategy is the best? I noticed that after migration from :truncation to :transaction my more than 800 examples run about 4 times faster!
  3. Should I turn off use_transactional_fixtures when I use database_cleaner :transaction?
  4. Is it true that the best strategy for rack_test is :transaction?
  5. What is the best practices for changing strategy on the fly from :transaction to :truncation when using selenium or akephalos?

P.S. Mysql, Rails 3, Rspec2, Cucumber

P.P.S. I know about spork and parallel_test and using them. But they are offtopic. For example, Spork save about 15-20 sec on whole suite run, but changing from :transaction to :truncation dramatically increase running time from 3.5 to 13.5 minutes (10 minutes difference).

解决方案

1., 2. & 4., You should use transactions (either with use_transactional_fixtures or transactions support from the database_cleaner gem) if you are using capybara's default engine, rack_test. As you noted, using transactions are substantially faster than using a truncation strategy. However, when database writes can go through different threads (as with selenium) transactions won't work. So you'll need to use truncation (or force everything to go through one db thread--another option).

3. Yes, you should turn off use_transactional_fixtures when using the database_cleaner gem since the gem natively support transactions. If you only need transactions then just use_transactional_fixtures and never load the database_cleaner gem.

5. The following code will switch between :transaction and :truncation on the fly. (Tested this with rspec, capybara, rails3.)

Features This should give you the best of both worlds. The speed of rack_test when you don't need to test javascript stuff and the flexibility of selenium when you do.

Also this code takes care of repopulating seed data in cases where it is needed (this method assumes you use seeds.rb to load your seed data--as is the current convention).

Add the following code to spec_helper.

config.use_transactional_fixtures = false
RSpec.configure do |config|
  config.before(:suite) do
    require "#{Rails.root}/db/seeds.rb"
  end

  config.before :each do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.strategy = :transaction
    else
      DatabaseCleaner.strategy = :truncation
    end
    DatabaseCleaner.start
  end
  config.after(:each) do
    if Capybara.current_driver == :rack_test
      DatabaseCleaner.clean
    else
      DatabaseCleaner.clean
      load "#{Rails.root}/db/seeds.rb"
    end
  end
end

Thanks Jo Liss for pointing the way.

PS: How to switch drivers on the fly

The above solution assumes you already know how to switch drivers on the fly. In case some who come here don't, here's how:

As above let's assume that you normally will use the default capybara driver rack_test, but need to use selenium to test some Ajaxy stuff. When you want to use the selenium driver use :js => true or @javascript for Rspec or cucumber respectively. For example:

Rspec example:

describe "something Ajaxy", :js => true do

Cucumber example:

@javascript
Scenario: do something Ajaxy

这篇关于Rspec,Cucumber:最好的速度数据库清理策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆