Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test to use existing database

Tags:

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.

like image 917
endre Avatar asked Jun 06 '11 09:06

endre


People also ask

What database does Django test use?

To help you get started, Django provides and uses a sample settings module that uses the SQLite database. See Using another settings module to learn how to use a different settings module to run the tests with a different database.

How does Django create test database?

By default pytest-django will set up the Django databases the first time a test needs them. Once setup, the database is cached to be used for all subsequent tests and rolls back transactions, to isolate tests from each other. This is the same way the standard Django TestCase uses the database.

What is RequestFactory in Django?

The request factory class RequestFactory. The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.


2 Answers

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.

like image 72
e4c5 Avatar answered Oct 24 '22 18:10

e4c5


This TEST_RUNNER works in Django 1.3

from django.test.simple import DjangoTestSuiteRunner as TestRunner  class DjangoTestSuiteRunner(TestRunner):     def setup_databases(self, **kwargs):         pass      def teardown_databases(self, old_config, **kwargs):         pass 
like image 20
Noel Pure Avatar answered Oct 24 '22 20:10

Noel Pure