Barney Szabolcs
Barney Szabolcs

Reputation: 12524

Django multi db test fails post teardown

My multi-db test problem is the following:

# (django 2.0.7, python 3.6)
# settings.py:

DATABASES = {
    'default':{}, 
    'one': { # connection1 settings here }
    'two': { # connection2 settings here }
}
DATABASE_ROUTERS = []

# test.py
class MyTestCase(TestCase):

    def test_my_function(self):
         pass # this IS literally the code

I run python manage.py test -v 2 and see that the test runner builds up the two mock databases and runs the test that is green

test_my_function (mymodule.test.MyTestCase) ... ok

Then ERROR then ERROR: test_get_pronunciation (languages.test_hr.HRTestCase)

with the following explanation:

File "/Users/Barnabas/PycharmProjects/rhymedict-multisite/venv/lib/python3.6/site-packages/django/db/backends/dummy/base.py", line 20, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

This should mean that the program tries to access the 'default' database.

(The same thing happens if I add multi_db = True to my MyTestCase)

Yet strangely if I write

class MyTestCase(TestCase):
    pass

The db's are built up, torn down just fine.

What do I do wrong?

Update:

self._post_teardown() raises the error and I can see that both dummy db's are torn down before, successfully.

Upvotes: 2

Views: 948

Answers (2)

Elias Prado
Elias Prado

Reputation: 1817

That is how I did to make tests work with two databases, worked like a charm:

import sys

if 'test' in sys.argv or 'test_coverage' in sys.argv: 
    DATABASES = {
        'default':{
            'ENGINE':'django.db.backends.sqlite3' # <---- you can add other db connections..
        }
    }
else:
    DATABASES = {
        'default': {},
            'one': { # connection1 settings here }
            'two': { # connection2 settings here }
    }

    # DB routers specification path - case sensitive:
    # ** needs to be within the same scope of DATABASES.
    DATABASE_ROUTERS = ['myapp.routers.db_router.MyRouter']

On tests file:

class TestModel(TestCase):

    databases = '__all__'

    . . .

Upvotes: 0

Barney Szabolcs
Barney Szabolcs

Reputation: 12524

Solution: I need to use SimpleTestCase instead of TestCase, and voila, problem solved.

The problem happened in _post_teardown() that should be an empty function (as it is empty in TestCase), but for some reason TransactionTestCase's _post_teardown() gets executed instead.

Upvotes: 2

Related Questions