[Answered ]-How to to override database settings in a Django TestCase

1👍

You need to overwrite _pre_setup and _post_teardown methods. In fact, there is a python package for the exact same purpose, that provides testing support for different databases with Django. You could use it if it serves your purpose, otherwise it can be used as a reference anyway.

Pypi Link:-

Django Test Addons

Documentation:-

Python Hosted

Read the docs

1👍

Ideally you should define a test_settings.py file like this

from settings import *
......
override whatever you want here
......

Then change the manage.py to something like this

#!/usr/bin/env python
import os
import sys


if __name__ == "__main__":
    test_settings = 'test_settings'
    settings = test_settings if 'test' in sys.argv else   'settings'
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings)

    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

This will ensure that test cases are run with your test_settings only to avoid any side-effect to the main database.

Leave a comment