# Testing using an existing test database

**URL:** <https://www.ruby-forum.com/t/testing-using-an-existing-test-database/137852>\
**Category:** Rails\
**Created:** [May 16, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/testing-using-an-existing-test-database/137852 "2008-05-16T22:15:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 16, 2008, 10:15pm UTC](https://www.ruby-forum.com/t/testing-using-an-existing-test-database/137852/1 "2008-05-16T22:15:25Z")

</div>

Ruby N. alert…

I tried searching for an answer for this question, but the search  
criteria weren’t very apparent. Here goes:

If you want to use an existing database to run your unit tests against,  
how do you do that? By default, it seems that Rails attempts to  
recreate the test database each time, but that’s not an option for me,  
because of the fact that I’m using enterprise databases – they’re too  
big to drop and recreate each time, even if I did have the permissions  
to do so (which I don’t). Currently, my unit test just seems to hang and  
do nothing, at least after the first run (when it appears to run and  
finish, but I’m not entirely sure).

Thanks very much!

---

<div class="post-metadata">

**Author:** ![noreaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c67d28/32.png) [@noreaster](https://www.ruby-forum.com/u/noreaster)\
**Post date:** [May 17, 2008, 2:59pm UTC](https://www.ruby-forum.com/t/testing-using-an-existing-test-database/137852/2 "2008-05-17T14:59:07Z")

</div>

Gabe H. wrote:

> Ruby N. alert…

Your secret is safe with us!

> If you want to use an existing database to run your unit tests against,  
> how do you do that?

If you have a nice database, and if you want to write unit tests that  
hit it  
as fixtures, and if you already have a few healthy unit tests, run this  
rake  
task:

namespace :test do  
desc ‘Create YAML test fixtures from data in an existing database.  
Defaults to development database. Set RAILS\_ENV to override.’

task :extract\_fixtures =\> :environment do  
sql = “SELECT \* FROM %s”  
skip\_tables = [“schema\_info”]  
ActiveRecord::Base.establish\_connection  
(ActiveRecord::Base.connection.tables - skip\_tables).each do  
|table\_name|  
i = “000”  
File.open(“#{RAILS\_ROOT}/tmp/#{table\_name}.yml”, ‘w’) do |file|  
data = ActiveRecord::Base.connection.select\_all(sql %  
table\_name)  
file.write data.inject({}) { |hash, record|  
hash[“#{table\_name}\_#{i.succ!}”] = record  
hash  
}.to\_yaml  
end  
end  
end

end

Don’t be fooled by imitations. Some versions of test:extract\_fixtures  
will  
overwrite your test/fixtures folder. That is absurd, because you should  
already have fixtures in there.

Next, look up a plugin called fixture\_scenarios. It lets you load a  
batch of  
fixtures from an alternate folder, not just the test/fixtures folder.

And remove records you don’t need from the “enterprise database” before  
extracting them - you never need a huge amount of records just to write  
tests.

And /obviously/ don’t test on the live database!

Your project probably got in this “enterprise” situation by not writing  
enough unit tests…

> By default, it seems that Rails attempts to  
> recreate the test database each time, but that’s not an option for me,  
> because of the fact that I’m using enterprise databases – they’re too  
> big to drop and recreate each time,

The main point of the fixture system is to write _unit_ tests. You need  
to  
think about one or two records, and a manipulation between them. A test  
case  
should use the pattern Assemble Activate Assert, and the fixtures are  
for  
the assembly part:

def test\_case  
thang = thangs(:Good\_Thang)  
thang.activate(42)  
assert{ thang.disposition() == :good }  
end

You should never need a billion records to do that. But if you load a  
billion records into your scenario, they will slow down testing.

> even if I did have the permissions  
> to do so (which I don’t).

If you don’t have permission to make a copy of the main database and use  
it  
for tests, quit.

> Currently, my unit test just seems to hang and  
> do nothing, at least after the first run (when it appears to run and  
> finish, but I’m not entirely sure).

Start with unit tests that only use a very few fixtures that you wrote  
by  
hand into your test/fixtures folder. Only after you get going with them  
should you then write tests that use scenarios of fixtures extracted  
from  
your production database.

–  
Phlip  
[O'Reilly Media - Technology and Business Training](http://www.oreillynet.com/onlamp/blog/2008/05/dynamic_languages_vs_editors.html)
