Running unittests on different databases

23 views
Skip to first unread message

varun naganathan

unread,
Dec 25, 2015, 3:09:22 AM12/25/15
to Django users
Hi,
I basically want to know how i  can run unittests on different databases like Postgres and Mysql.The documnetation says something about writing a custom settings file.However I'm unable to actually understand how actually the settings file must look.
Could someone perhaps post a sample file ??

Mike Dewhirst

unread,
Dec 25, 2015, 6:55:49 AM12/25/15
to django...@googlegroups.com
On 25/12/2015 7:09 PM, varun naganathan wrote:
> Hi,
> I basically want to know how i can run unittests on different
> databases like Postgres and Mysql.The documnetation says something about
> writing a custom settings file.However I'm unable to actually understand
> how actually the settings file must look.
> Could someone perhaps post a sample file ??

It is quite simple and fully documented in Two Scoops of Django which I
recommend with enthusiasm.

One of the options in testing is --settings=<whatever> where you can
nominate any settings file you like. For example settings.test_mysql

To follow the Two Scoops recipe you create a settings directory and a
base settings file with all the settings which are common to all
scenarios you wish to run. Hence you might have basic settings specified
in ../settings/base.py - including your regular database settings eg.,

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': <database_name>,
'USER': <database_user>,
'PASSWORD': <database_pass>,
'HOST': <database_host>,
'PORT': database_port>,
}
}

... all exactly as set out in Django docs which I'm sure you have
already studied.

Then in the same directory you have eg., test_mysql.py which needs to
import the base.py settings into its own (ie., test_mysql) namespace
like this ...

from __future__ import absolute_import
from .base import *

# now overwrite/override particular base.py settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.<mysql_interface*>',
'NAME': <database_name>,
'USER': <database_user>,
'PASSWORD': <database_pass>,
'HOST': <database_host>,
'PORT': database_port>,which take your fancy.
}
}

* Don't know what that is because I don't use it.

So this lets you name any settings file - provided that it imports the
settings you don't wish to rewrite - and in which you can rewrite any
settings or even add other settings or other code which suits your needs.

To continue the recipe I have a settings/production.py which imports
"base" in the same manner and adds its own changes (if any). Similarly,
settings/mike for my own development settings where different than those
in base.py and so it goes.

All the best

Mike

>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/f2f08c73-83ea-4f68-8fca-70e64d97b25d%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/f2f08c73-83ea-4f68-8fca-70e64d97b25d%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

varun naganathan

unread,
Dec 27, 2015, 5:11:04 AM12/27/15
to Django users
Thanks a lot.That really helped.
Reply all
Reply to author
Forward
0 new messages