Changing the active database in django
i'm wr开发者_JAVA技巧iting a testing application that i'm using to test the rest of my code base. What i'd like to be able to do for it is when i test using this manage.py command, automatically change to be logging to a different database. is there a good way to do this?
Django automatically creates and drops a test database for you. Unless otherwise specified (we'll see how to in a second) this will be test_
+ <the name of the database in the settings file>
. So if your settings
uses database foo
, the tests will be executed against test_foo
. No configuration changes are needed for this.
If you wish to execute tests against a custom database (rather than test_foo
) you can do that by tweaking the TEST_NAME
setting. You can add TEST_NAME
to each dictionary in DATABASES
.
Create a testing version of settings.py
, and specify it on the command line when you run your test:
$ python manage.py test --settings=settings_test
精彩评论