Speeding up Django Testing

Viewed 6075

Im looking to learn more about your testing flows with Django.

Background information http://docs.djangoproject.com/en/dev/topics/testing/

Im encountering difficulties when using test driven development. The test runner of Django constantly creates all db models in a test db when starting. For our current projects (between 40 and 240 models) this means it takes easily 20s for tests to start.

This makes it completely unworkable for testing a new feature often. My question, how do you guys work around this?

I've tried a few things in the past a.) - change the testloader to reuse the same test db every time and apply migrations when needed b.) - run my unit tests from within the __main__ flow of python files

option b is awkward with the sys.path, option a is doable but doesnt seem to be the django way.

Update: Option A is indeed not such a bad solution. Its just quite a bit of effort. Which makes me believe people use a different workaround. SQL lite could be that workaround. But im guessing there are more.

8 Answers

I have found another way to speed up testing. The most time consuming operation is writing to/reading from hard drive (I'm using sqlite for testing). The solution is to create ramdisk, and put the sqlite database file there. I have reduced testing time by factor of 10.

Creating ramdisk:

#!/bin/sh

mkdir -p /tmp/ramdisk; chmod 777 /tmp/ramdisk
mount -t tmpfs -o size=256M tmpfs /tmp/ramdisk/

Changing db file path:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/tmp/ramdisk/test.db',
        'TEST_NAME': '/tmp/ramdisk/test.db',
    }
}
Related