开发者

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

开发者 https://www.devze.com 2022-12-31 11:40 出处:网络
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

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:
  ...
开发者_Python百科

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

Thanks!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消