Tutorial :Can Rails unit tests be run on a different environment than test?



Question:

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!


Solution:1

As of right now this still does not work. I've submitted an issue: https://github.com/rails/rails/issues/21478.

I'll edit this answer with updates


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »