Reputation: 334
I'm quite newbie in Django. I have an application in project which is developed with test driven development. In production we use MySQL as database engine, but if I run test with MySQL:
./manage test myapp
Then it absorbs too much time (2-4 mins) to create database, althought tests are quite fast (about a second).
If I use SQLite then tests requires only a few seconds, which is perfect for me. But the problem is that I often need the admin interface with my local database on MySQL.
How to make django to run tests on sqlite and launch runserver with mysql?
Now I use these settings in settings/local.py, but I should comment/uncomment lines to change database depending on what activity I do at the moment.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Use for testing
'NAME': 'databasename.db3',
# 'ENGINE': 'django.db.backends.mysql', # Use if need admin on localserver
# 'NAME': 'databasename',
'USER': 'myuser', # Not used with sqlite3.
'PASSWORD': 'somepassword', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'TEST_CHARSET': "utf8", #option to make tesing database with utf8
'TEST_COLLATION': "utf8_general_ci",
}
}
Upvotes: 8
Views: 4992
Reputation: 136
You don't need to do any hacks. "--settings" - this is what you're looking for.
python manage.py test APP --settings settings.local
python manage.py test APP --settings settings.production
Upvotes: 6
Reputation: 1712
Usually i use the following schema:
Create a two settings files for different modes
touch settings/production.py
touch settings/testing.py
On production mode
ln -s settings/production.py settings/__init__.py
On testing mode
ln -s settings/testing.py settings/__init__.py
Upvotes: 0
Reputation: 26848
Whenever possible it's always a good idea to use the same setup in production and test. It makes for better testing. I great way to deal with tests running slowly is to use django-nose. It lets you reuse previously created test databases. This can super speed up test runs. Check out enabling database reuse in the docs. An example is as follows:
REUSE_DB=1 ./manage.py test
Upvotes: 0
Reputation: 474161
One option would be to check sys.argv
for the test
argument in settings.py
:
if 'test' in sys.argv:
DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
DATABASES['default']['NAME'] = 'databasename.db3'
Though, as a side note: strictly speaking, it is not really a good idea to have different database backends for testing and for development/stage/production. You may encounter database-specific "special" cases, that can cost you a lot of time and headache.
Upvotes: 12