Robert
Robert

Reputation: 325

In Django multiple databases should not creat a test database by manage.py on 1 specific database

In Django I'm using multple databases, but by testing the functionality 1 database of the multiple database should not create a test database if you run manage.py test **. How can I solve this issue.

Upvotes: 1

Views: 763

Answers (1)

Aaron Lelevier
Aaron Lelevier

Reputation: 20838

In your settings.py you can tell Django what database to use when testing. Here is the code to do so if you would like to use SQLite for example:

settings.py:

if 'test' in sys.argv:
    DATABASES['default'] = {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'tests.db',
    }

Django creates a new database every time you run your manage.py test, so you would want to use fixtures in your test.py file to preload you database with data before running tests (if you need it preloaded with data).

To generate a fixture for the User model for example, use this code:

python manage.py dumpdata auth.User --indent=2 > app_name/fixtures/admin.json

To use fixtures you would structure your tests.py like so:

tests.py:

from django.test import TestCase

class MyTests(TestCase):
    fixtures = [admin.json]

    def setUp(self):
        # other setup code here

    def test_the_obvious(self):
        assert True == True

Upvotes: 2

Related Questions