Use SQLite file database for tests (and not in-memory)

417 views
Skip to first unread message

Daniel Gilge

unread,
Mar 10, 2018, 4:05:27 PM3/10/18
to Django users
Hi,

Introduction:

Django uses an in-memory database for testing when it finds a SQLite database in the settings. However, the ChannelsLiveServerTestcase (of Channels 2.0) cannot be used with an in-memory database.

It was nice to still be able to use SQLite, because it doesn't have any dependencies and therefore it is the best database to be used for tests which have to work on all kind of systems (as far as I can see). I looked through the Django code to find the part where it is determined whether to use an in-memory database or not but I couldn't find it.

Question:

Could somebody give me a hint how to tell Django to use a SQLite database on filesystem for tests, please?

Regards,

Daniel

Daniel Gilge

unread,
Mar 10, 2018, 4:27:25 PM3/10/18
to Django users
Ok, I found it. You just have to define a name for your test database in your settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'mydatabase',
        'TEST': {
            'NAME': 'test_database',
        },
    },
}

Thanks!
Reply all
Reply to author
Forward
0 new messages