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?
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