开发者

Django test runner ignoring the --settings option

I'm trying to use a different set of configs for testing my django app (like, using in-memory sqlite3 instead of mysql) and for this purpose I'm trying to use another settings module. I run my tests this way:

python manage.py test --settings=test_settings

But Django seems 开发者_Go百科to ignore my test_settings module. Am I doing something wrong?


Is test_settings a file or a directory?

Here's how I load different settings for tests. In settings.py, at the bottom:

# if manage.py test was called, use test settings
if 'test' in sys.argv:
    try:
        from test_settings import *
    except ImportError:
        pass

Super bonus! If you want to use sqlite3 for tests, you should activate integrity constraints, so you get the same foreign key exceptions as with mysql (lost of lot of time with this one). In some file of your project:

from django.db.backends.signals import connection_created
def activate_foreign_keys(sender, connection, **kwargs):
    """Enable integrity constraint with sqlite."""
    if connection.vendor == 'sqlite':
        cursor = connection.cursor()
        cursor.execute('PRAGMA foreign_keys = ON;')

connection_created.connect(activate_foreign_keys)


I just bumped into this and confirm that on Django 1.6.1 --settings option is ignored. I filed a ticket on the Django bug tracker: https://code.djangoproject.com/ticket/21635#ticket

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜