2

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 to ignore my test_settings module. Am I doing something wrong?

2 Answers 2

2

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

test_settings is a Python module (test_settings.py). Thank you for the explanation of transactions. I don't really use transactions in this project, do I need to do this second code?
Ooops! I meant 'foreign key constraints', no 'transactions'. Edited my answer.
1

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

1 Comment

Yes, I know. That's why I need to change manage.py manually from now. It is not optimal, but I need to get this working :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.