12

Just wondering if there's a way to run Rails tests without dropping the database. I'm currently only executing unit tests and am using the following rake command to do so: rake test:units.

Thanks for the help in advance!

Just in case this is relevant:

  • Rails 3
  • Ruby 1.8.7 (MRI)
  • Oracle 11g Database
    • activerecord-oracle_enhanced-adapter
John
  • 9,254
  • 12
  • 54
  • 75

6 Answers6

6

In Rails 5 (and possibly earlier versions), just comment-out the following line in spec/rails_helper.rb:

 ActiveRecord::Migration.maintain_test_schema!

This will prevent rake test or rspec from attempting to drop your test DB. You'll need to run migrations manually as well.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
5

For Rails 5.2 this behaviour can be modified setting maintain_test_schema to false in test/test_helper.rb before importing rails/test_help:

ActiveRecord::Base.maintain_test_schema = false
require "rails/test_help"

rails/test_help will check the value of maintain_test_schema to decide if it has to drop/create/migrate the test database or not.

torce
  • 197
  • 3
  • 9
3

After doing some research, I have found that there isn't a way to do this. The test rake tasks will always drop the database, even when providing the TEST= option as Bohdan suggests.

By using the --trace option, this can be proven. Here is the output:

$ rake test:units TEST=test/unit/post_test.rb --trace
(in /Users/johnnyicon/Development/ror/test-app)
** Invoke test:units (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment 
** Execute db:test:purge
** Execute db:test:load
** Invoke db:schema:load (first_time)
** Invoke environment 
** Execute db:schema:load
** Execute test:prepare
** Execute test:units

Reading through the Ruby on Rails Guides for Testing, it describes what some of these rake tasks mean. The one to pay particular attention to is the db:test:load task, which you see on the 7th line from the bottom of the output as ** Execute db:test:load. The guides say the following about this task:

Recreate the test database from the current schema.rb

So even if I were to execute the unit tests one by one as Bohdan suggests, the rake task would still recreate the database. Not the answer I was hoping for, but it isn't a problem anymore.

The reason I was asking to begin with was because I did not have access to another database to use for testing, so I was using my development database for testing as well. But since then, I've been able to get another database dedicated for testing.

Thanks anyway Bohdan! I appreciate the help!

John
  • 9,254
  • 12
  • 54
  • 75
2

This is a rather old post that does the monkey patching of overriding purge/load tasks: http://www.pervasivecode.com/blog/2007/09/22/making-rails-raketest-not-drop-your-pgsql-database/

hammady
  • 969
  • 1
  • 13
  • 22
  • 1
    Similar answers can be found here: http://stackoverflow.com/q/24785428/680464 ---- and here: http://stackoverflow.com/q/8112074/680464 – juanitogan Sep 29 '14 at 20:04
2

For those looking for a way to skip Rails' default behavior, try adding this to your Rakefile:

Rake::Task["db:test:prepare"].clear
Rake::Task["db:test:load"].clear
Rake::Task["db:test:purge"].clear
mpelzsherman
  • 707
  • 9
  • 9
-1

Could you not write a custom Rake task that monkey patched the Rake db:test:load task to do nothing?

Allan
  • 671
  • 1
  • 5
  • 8