Django test to use existing database

endre picture endre · Jun 6, 2011 · Viewed 8.3k times · Source

I'm having a hard time customizing the test database setup behavior. I would like to achieve the following:

  • The test suites need to use an existing database
  • The test suite shouldn't erase or recreate the database instead load the data from a mysql dump
  • Since the db is populated from a dump, no fixtures should be loaded
  • Upon finishing tests the database shouldn't be destroyed

I'm having a hard time getting the testsuiterunner to bypass creation.

Answer

e4c5 picture e4c5 · May 8, 2016

Fast forward to 2016 and the ability to retain the database between tests has been built into django. It's available in the form of the --keep flag to manage.py

New in Django 1.8. 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.

This pretty much fullfills all the criteria you have mentioned in your questions. In fact it even goes one step further. There is no need to import the dump before each and every run.