I want to be able to use an existing test database to run my tests against and not have Django create and delete a database every time I want to run the tests. Is this possible?
-
why would you want that? You also want to test the database creation ... – Gabi Purcaru Jan 05 '11 at 16:55
-
8Not when it takes hours to build. – Ryan Detzel Jan 05 '11 at 17:12
-
You can choose different DB engine for tests (sqlite has way faster DB creation) – Mikhail Korobov Jan 06 '11 at 12:47
-
Another approach is /not/ to use `./manage.py test`, but write a management command: http://stackoverflow.com/questions/1646468/ – Stefano Jun 19 '14 at 16:29
2 Answers
It's possible, here is a way :
1) Define your own test runner look here to see how.
2) For your custom test runner look in the default test runner, you can just copy and past the code and just comment this line : connection.creation.destroy_test_db(old_name, verbosity)
which is responsible for destroying the test database, and i think you should put the connection.creation.create_test_db(..)
line in a try except something like this maybe:
try:
# Create the database the first time.
connection.creation.create_test_db(verbosity, autoclobber=not interactive)
except ..: # Look at the error that this will raise when create a database that already exist
# Test database already created.
pass
3) Bound TEST_RUNNER in setting.py to your test runner.
4) Now run your test like this: ./manage.py test

- 713
- 1
- 17
- 26

- 67,571
- 18
- 114
- 106
-
So that looks good but I still need to setup the connection to the local test db no? – Ryan Detzel Jan 05 '11 at 21:08
-
@Ryan Detzel: i just edited my answer for more detail , about your question i think with the code above the test database will be created the first time that the test are run yes, this test database is created atomically by Django look here : http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#the-test-database – mouad Jan 05 '11 at 21:39
-
@mouad, can i configure it so that it does not even load the fixtures / flush it? – Sandeep Raju Prabhakar Jul 16 '13 at 21:35
Who are using Django >= 1.8
python manage.py test --keepdb
--keepdb Preserves the test database between test runs. This has the advantage of skipping both the create and destroy actions which can greatly decrease the time to run tests, especially those in a large test suite. If the test database does not exist, it will be created on the first run and then preserved for each subsequent run. Any unapplied migrations will also be applied to the test database before running the test suite.

- 1,570
- 19
- 22