Overriding TEST_RUNNER with @override_settings

1.1k views Asked by At

In order to run my tests faster I created a no db test runner as in this answer. Then I needed to set the TEST_RUNNER settings variable to my new test runner but only for certain tests. To achieve this, I tried using django.test.utils.override_settings decorator like this (as in the docs):

from django.test import TestCase
from django.test.utils import override_settings

class MyTestCase(TestCase):
    @override_settings(TEST_RUNNER='path_to_my_no_db_test_runner')
    def test_my_test_case(self):
        ...

The problem is that when I run this test, django will still create the database, which is not the expected behavior of course. The curious thing is that if I set the TEST_RUNNER directly in my settings.py it works perfectly, but with django.test.utils.override_settings it seems to have no effect. I also tried using this override_settings module but got the same results.

What am I dping wrong? Is there any other way of achieving this? I'd rather not to create a test_settings.py and run my tests with --settings argument.

1

There are 1 answers

0
Antwan On

Put this piece of code in your config :

TESTING = 'test' in sys.argv

...

if TESTING:
    TEST_RUNNER = 'path_to_my_no_db_test_runner'
    DATABASES = {}

The TESTING setting will be defined only when you run tests, you can then dynamically change some settings including your DB, migrations, test runners...

It will be loaded at the very beginning of Django initialisation therefore if you override DATABASES no DBs will be created.