views:

28

answers:

1

Hi,

We have a large multi-developer project under Rails in which we are using tests for both models and controllers. Right now the developers have to switch the DB parameters for the 'test' environment to match their local dev environments before running tests. I am wondering if there is a way to run those tests on any environment other than 'test'?

For example we have in database.yml:

test:
  database: ...
  host: ...
  username: ...
  password: ...
...
dev-one:
  ...
dev-two:
  ...

I can't find anything in the docs on this but maybe I am looking in the wrong place. Any ideas?

Thanks!

+1  A: 

Just a matter of specifying the environment explicitly when you're going to run the tests. You just need a couple of preparations beforehand.

Say your new environment is going to be named "testjohn" (presumably for a developer named John). Then:

1- Copy config/environments/test.rb to config/environments/testjohn.rb

2- Add the corresponding DB stanza to config/database.yml (copy it from the test stanza, rename it, then presumably change the database name, password, and other data). My (rather simplistic) example uses this:

testjohn:
  adapter: sqlite3
  database: db/testjohn.sqlite3
  pool: 5
  timeout: 5000

3- Run your tests as follows:

RAILS_ENV="testjohn" rake db:migrate
RAILS_ENV="testjohn" rake test:units

Incidentally, since RAILS_ENV is just an environment variable, you might have a script that sets it beforehand, or even have each developer configuring his own RAILS_ENV variable in his .profile or whatever file. That way they just run rake test:units and tests automagically execute under their personalized environment.

Roadmaster
Appreciate the response, however, that is exactly what I have tried and it does not work. The environment that you specify in RAILS_ENV is indeed loaded in the context of the rake task, however, once ruby executes your tests it silently switches to your test environment and purges the test database and loads your fixtures.After more research it seems that this behavior is coded pretty deep into Test::Unit and I am sort of giving up on the possibility. But still very open to suggestions.
futureal