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

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

问题描述

我想提高我的测试速度.

I would like to increase the speed of my tests.

  1. 我应该使用 use_transactional_fixtures 还是使用 database_cleaner gem?
  2. 哪种 database_cleaner 策略最好?我注意到从 :truncation 迁移到 :transaction 后,我的 800 多个示例运行速度提高了大约 4 倍!
  3. 在使用database_cleaner :transaction 时是否应该关闭use_transactional_fixtures?
  4. rack_test 的最佳策略是 :transaction 是真的吗?
  5. 在使用 selenium 或 akephalos?
  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?

附言Mysql、Rails 3、Rspec2、Cucumber

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

P.P.S.我知道 sporkparallel_test 并使用它们.但它们是题外话.例如,Spork 在整个套件运行中节省了大约 15-20 秒,但是从 :transaction 更改为 :truncation 将运行时间从 3.5 分钟显着增加到 13.5 分钟(10 分钟差异)).

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., 如果您使用 capybara 的默认引擎 rack_test,您应该使用事务(使用 use_transactional_fixtures 或 database_cleaner gem 的事务支持).正如您所指出的,使用事务比使用截断策略要快得多.但是,当数据库写入可以通过不同的线程(如 selenium)时,事务将不起作用.因此,您需要使用截断(或强制所有内容都通过一个数据库线程——另一种选择).

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. 是的,您应该在使用 database_cleaner gem 时关闭 use_transactional_fixtures,因为 gem 本身支持事务.如果您只需要事务,那么只需使用_transactional_fixtures 并且从不加载 database_cleaner gem.

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. 以下代码将在 :transaction:truncation 之间动态切换.(使用 rspec、capybara、rails3 对此进行了测试.)

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

功能这应该可以让您两全其美.rack_test速度,当您不需要测试 javascript 内容时,selenium灵活性.

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.

此外,此代码还负责在需要时重新填充种子数据(此方法假定您使用seeds.rb 加载种子数据——这是当前的惯例).

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).

将以下代码添加到 spec_helper.

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

谢谢乔·利斯 指路.

PS:如何即时切换驱动程序

上述解决方案假设您已经知道如何即时切换驱动程序.如果有些人不来这里,方法如下:

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:

如上所述,假设您通常会使用默认的 capybara 驱动程序 rack_test,但需要使用 selenium 来测试一些 Ajaxy 的东西.当您想使用 selenium 驱动程序时,请使用 :js =>分别为 Rspec 或 Cucumber 为 true@javascript .例如:

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 示例:

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

黄瓜示例:

@javascript
Scenario: do something Ajaxy

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

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