dimanche 7 février 2016

Django test database not being used with custom test runner

I am using Django 1.9 and have written a custom test runner. I cannot figure out why my production database is being used instead of my test_ database. I've read quite a lot of the Django docs to no avail. The name of my production database is zippymeals, so my test database should be test_zippymeals. But my tests seems to be running against the zippymeals database, not test_zippymeals. Here is the code for my custom test runner:

class TestRunner(DiscoverRunner):

def __init__(self, pattern=None, top_level=None, verbosity=1, interactive=True, failfast=False, **kwargs):
    super(TestRunner, self).__init__(pattern, top_level, verbosity, interactive, failfast, **kwargs)

def setup_databases(self, **kwargs):
    bash_cmd = "createdb -T zippymeals_template test_zippymeals"
    process = subprocess.Popen(bash_cmd.split(), stdout=subprocess.PIPE)
    process.communicate()[0]

def teardown_databases(self, old_config, **kwargs):
    bash_cmd = "dropdb test_zippymeals"
    process = subprocess.Popen(bash_cmd.split(), stdout=subprocess.PIPE)
    process.communicate()[0]

and I set the following in my settings.py file:

TEST_RUNNER = 'tests.test_runner.TestRunner'

I'm using PostgreSQL, so my custom test runner uses the zippymeals_template database (which is an empty structure of my database) to create an empty test_zippymeals database. The test_zippymeals database gets created fine, it's just not being used when I run my tests. Also, I've tried doing the following with no luck:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'zippymeals',
        'TEST': {
            'NAME': 'test_zippymeals'
        }
    }
}

Does anyone know how to inherit from DiscoverRunner and ensure that the test_ database is being used?

Aucun commentaire:

Enregistrer un commentaire