2

Hi I would like to create a separate empty database for tests. I read on django docs (https://docs.djangoproject.com/en/2.1/topics/testing/overview/) that :

The default test database names are created by prepending test_ to the value of each NAME in DATABASES. When using SQLite, the tests will use an in-memory database by default (i.e., the database will be created in memory, bypassing the filesystem entirely!). The TEST dictionary in DATABASES offers a number of settings to configure your test database. For example, if you want to use a different database name, specify NAME in the TEST dictionary for any given database in DATABASES.

So I tried:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'test_db': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'test_db.sqlite3'),
    }
}

but tests still use the default database when running them with

./manage.py test

How can I create and specify a new, empty database for tests purposes?

1
  • No, the database is, like the specifications say, not default, but test_default, so it already creates a test database without specifying one. Commented Sep 2, 2018 at 17:18

1 Answer 1

3

I think you misread the documentation. Django automatically uses a separate database.

Say your config file looks like:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

Then Django will not use the 'default' database, if the name of the database is 'FOO', it will create a database with the name test_FOO. Such that testing and running the Django project should - without of course "patching" this behaviour - not interfere (at least not the databases).

If you however want to specify a different NAME (or other attributes), you can add a 'TEST' key [Django-doc] in the databases, like:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'TEST': {
            'NAME': os.path.join(BASE_DIR, 'other_db.sqlite3'),
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks so much! But when I run my selenium tests I can see that the database is populated with data from the default database. And when I create objects for instance a new user int the test and try to log in the new account, the action fails. Do you maybe have any ideas why this might be?
@madasionka: no that is very strange. Perhaps it is worth sharing all (relevant) settings. Normally by using sqlite3, as said in the documentation, there is no file stored in the filesystem: the database is stored in memory. So all queries should make no changes to the file system at all.
I looked carefully at my settings and I don't see anything suspicious. The weird thing is that it would seem that indeed the tests are somehow using a different database (since if i create a user it doesn't get created int the default database), but the chromium window with my application is using the default database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.