DATABASES = { # 'default': { # 'ENGINE': 'postgresql_psycopg2', # ... # } # for unit tests 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'mydatabase' } }
I have two databases: one I'd like to use for unit tests, and one for everything else. Is it possible to configure this in Django 1.2.4?
(The reason I ask is because with postgresql I'm getting the following error:
foo@bar:~/path/$ python manage.py test Creating test database 'default'... Got an error creating the test database: permission denied to create database Type 'yes' if you would like to try deleting the test database 'test_baz', or 'no' to cancel: yes Destroying old test database... Got an error recreating the test database: database "test_baz" does not exist
Why could I be getting this error? I guess I don't really care if I can always use SQLite for unit tests, as that works fine.)
Writing tests Django's unit tests use a Python standard library module: unittest .
Django's admin doesn't have any explicit support for multiple databases. If you want to provide an admin interface for a model on a database other than that specified by your router chain, you'll need to write custom ModelAdmin classes that will direct the admin to use a specific database for content.
Django provides a test framework with a small hierarchy of classes that build on the Python standard unittest library. Despite the name, this test framework is suitable for both unit and integration tests. The Django framework adds API methods and tools to help test web and Django-specific behavior.
In your settings.py
(or local_settings.py
):
import sys if 'test' in sys.argv: DATABASES['default'] = { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'mydatabase' }
The way I handle this is through having multiple settings files, since I use that to maintain a set of common settings with modifications for each instance. It's a little more complicated to set up than some of the other solutions, but I needed to do it anyway because I was managing slightly different settings for local development, remote development, staging and production.
https://code.djangoproject.com/wiki/SplitSettings has a number of options for managing settings, and I've chosen a practice similar to the one described at https://code.djangoproject.com/wiki/SplitSettings#SimplePackageOrganizationforEnvironments
So, in my Django project directory, I have a settings folder that looks like this:
$ tree settings settings ├── defaults.py ├── dev.py ├── dev.pyc ├── __init__.py ├── lettuce.py ├── travis.py ├── unittest.py
The common settings are in settings/defaults.py and I import these in my instance settings files. So settings/unittest.py looks like this:
from defaults import * DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': 'my_database', } }
Then, when I want to run tests, I just execute:
$ ./manage.py test --settings=settings.unittest
to use sqlite for testing. I'll use a different settings module if I want to use a different test runner or database configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With