Running django tests with sqlite

Viewed 18077

I use Postgres for production and development, but I'd like to use sqlite to run some tests. I don't see an easy way to configure one engine for tests and another for dev / production. Am I missing something?

5 Answers

I've end up by adding the following in my settings.py. The --keepdb will setup the Sqlite DB in RAM.

if 'test' in sys.argv:
    for db_test in ['default']: # Add other DBs if needed
        DATABASES[db_test]['ENGINE'] = 'django.db.backends.sqlite3'
        if '--keepdb' in sys.argv:
            DATABASES[db_test]['TEST']['NAME'] = '/dev/shm/' + db_test + '.test.db.sqlite3'

I think modifying the settings.py with if 'test' in sys.argv as suggested is a hack and doesn't work for example when you want multi-threaded test execution in pytest. I think a better way would be to create a separate settings_test.py and adding DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3' to it.

When using Django's testframework, execute your tests with python manage.py test --settings=myapp.settings_test

When using pytest, create a pytest.ini and insert

[pytest]
 DJANGO_SETTINGS_MODULE = myapp.settings_test
Related