eugene
eugene

Reputation: 41745

Django py.test run on real database?

py.test sets up a test database.

I'd like to use the real database set in settings.py file.
(Since I'm on test machine with test data already)

Would it be possible?

Upvotes: 1

Views: 2466

Answers (3)

Aaron Melgar
Aaron Melgar

Reputation: 372

Using an existing external database is documented here, though I agree with Ederson: it's better practice to use a fixture so your tests run in all environments including production.

Upvotes: 0

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15400

I think Ederson Badeca right, you shouldn't use your project_db as test_db. Instead i would propose creating copy of current project_db on database setup and running tests against it instead. In django tests you would need to create custom test runner to do so. But in py.test there is fixture called db which does all db setup/teardown logic. So you would need to create custom db fixture that will do what you want. Here is source of default db fixture https://github.com/pytest-dev/pytest-django/blob/master/pytest_django/db_reuse.py

P.S. If you still want to run test on project_db.

I've done this only in django default tests with custom runner bellow(django version 1.6, haven't tested in other versions)

# coding: utf-8

from __future__ import unicode_literals, absolute_import

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.db import connection
from django.test.runner import DiscoverRunner, setup_databases
from django.test.testcases import connections_support_transactions

# configure(default_settings=settings, 
settings.DEBUG = True
settings.TEMPLATE_DEBUG = True


class KeepDatabaseTestRunner(DiscoverRunner):
    """
    Skips creating test database and uses default database for tests.
    """

    def setup_databases(self, **kwargs):
        """Setups databases to run and migrate on settings.DATABASES['default']['TEST']['NAME']"""
        old_name = settings.DATABASES['default']['NAME']
        try:
            test_name = settings.DATABASES['default']['TEST']['NAME']
        except KeyError:
            raise ImproperlyConfigured("TEST NAME is not defined in settings.DATABASES['default']")
        connection.settings_dict["NAME"] = test_name
        connection.settings_dict['SUPPORTS_TRANSACTIONS'] = connections_support_transactions()
        call_command('syncdb')
        call_command('migrate')
        return [connection, old_name, False], []

    def teardown_databases(self, old_config, **kwargs):
        """Will not do anything"""
        pass

Update

Ok, here is the base idea how you could do it. Firstly you would have to completely ovreload default db fixiture. The line of code you are looking for is https://github.com/pytest-dev/pytest-django/blob/master/pytest_django/fixtures.py#L39. This is where py.test does it's test_db name assigning _get_test_db_name. So basically you need to create custom _get_test_db_name and in it you should supply path to your project_db. Also after that you would have to use --reuse-db when running tests.

P.S. You also could fill test_db with your project_db's dump and after that run tests using --reuse-db and py.test will run your test against recent project_db version which is in test_db.

Upvotes: 3

Ederson Badeca
Ederson Badeca

Reputation: 377

yeah you can override the settings on the setUp set the real database for the tests and load your databases fixtures.. but I think, it`s not a good pratice, since you want to run yours tests without modify your "real" app env.

you should try the pytest-django.

with this lib you can reuse, create drop your databases test.

Upvotes: 0

Related Questions