0

I've got an application that is running Django 2 that connects to a Microsoft SQL Server backend.

We're in a big environment where things are pretty tightly controlled - a user account has access to the database that houses all the tables for my django application.

So I have found through various posts that I need to create a testrunner for my application - which is fine. I can override the setup_databases function - but I'm not sure exactly how to do that.

My TestRunner(DiscoverRunner) looks like this:

class ExistingDbTestRunner(DiscoverRunner):
    """ A test runner to test without database creation """

    def setup_databases(self, **kwargs):
        """ Override the database creation defined in parent class """
        # force Django to connect to the correct db for tests
        connection = self.connections['default']
        db_conf = connection.settings_dict
        connection.connect()

    def teardown_databases(self, old_config, **kwargs):
        """ Override the database teardown defined in parent class """
        pass

But this fails with AttributeError: 'ExistingDbTestRunner' object has no attribute 'connections'

I'm just trying to get this to use the 'default' database that I have set in the settings for testing purposes.

It's worth noting - the default database specified in settings is a duplicate copy of the production database with a different name.

So I just want my tests to run against this duplicate database. What should I be changing so it connects?

Hanny
  • 2,078
  • 6
  • 24
  • 52

1 Answers1

0

Django tests do not operate on existing database or are meant to be used that way. Django always creates a new database, with the name test_db_name_specified_in_settings for all its tests.

More documentation can be found here: https://docs.djangoproject.com/en/2.0/topics/testing/overview/#the-test-database

doubleo
  • 4,389
  • 4
  • 16
  • 19
  • According to [the docs](https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-DATABASE-TEST) I thought that's what the `TEST` setting was for, no? I've started playing with it - but because of our unique setup have ran into some things, but the `TEST` setting with `--keepdb` flag works. – Hanny May 29 '18 at 14:06
  • --keepdb keeps the test_ db that django created.Your requirement was to use DEFAULT db you set in the settings. – doubleo May 29 '18 at 20:47
  • 1
    is it not possible to use pytest-django to use a read-only db for testing? – r_zelazny Dec 20 '18 at 17:45