Run Rails tests without losing the test database

Just wondering if there is a way to run Rails tests without dropping the database. Currently, I only run unit tests and use the following rake command to do this: rake test:units .

Thanks for the help in advance!

Just in case, this is true:

  • Rails 3
  • Ruby 1.8.7 (MRI)
  • Oracle Database 11g
    • ActiveRecord-oracle_enhanced adapter
+6
ruby-on-rails unit-testing oracle11g ruby-on-rails-3 testing
source share
5 answers

After some research, I found that there is no way to do this. The tasks of the test rake will always delete the database, even if you provide the TEST= option, as Bogdan suggests.

Using the --trace , this can be proven. Here is the result:

 $ 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 

After reading Ruby on Rails Guides for Testing , he describes what some of these tasks mean. The db:test:load task, which you see on the 7th line at the bottom of the output as ** Execute db:test:load deserves special attention. Guides say the following about this task:

Restore test database from current schema.rb

So, even if I were to perform unit tests one by one, as Bogdan suggests, the rake task will recreate the database anyway. Not the answer I was hoping for, but this is no longer a problem.

The reason I asked to start was because I did not have access to another database for testing, so I also used my development database for testing. But since then I was able to get another database designed for testing.

Thank you Bogdan! I appreciate the help!

+2
source share

This is a pretty old post that does a monkey fix for redefining cleanup / upload tasks: http://www.pervasivecode.com/blog/2007/09/22/making-rails-raketest-not-drop-your-pgsql-database/

+2
source share

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 to reset the test database. You will also need to perform manual migration.

+1
source share

Could you write a custom Rake task so that the monkey pays Rake db: test: load task do nothing?

0
source share

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

 Rake::Task["db:test:prepare"].clear Rake::Task["db:test:load"].clear Rake::Task["db:test:purge"].clear 
0
source share

All Articles