Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify Django Test Database names in settings.py

I'm specifying the databases using a python object:

DATABASES = {  'default':{    'ENGINE':'mysql',    'NAME':'testsqldb',    'USER':'<username>',    'PASSWORD':'<password>',  },  'dynamic_data':{    'ENGINE': 'sqlite3',    'NAME':'',    'USER':'',    'PASSWORD':''  }, } 

How can I specify the name of my test database? I've been trying to use TEST_NAME = 'auto_tests' in the settings.py file. However, when I run python manage.py tests <app_name> I get the following message:

Creating test database 'default'... Got an error creating the test database: (1007, "Can't create database 'test_testsqldb'; database exists") Type 'yes' if you would like to try deleting the test database 'test_testsqldb', or 'no' to cancel: 

I'm expecting the system to create a separate database when running my tests, presumably called 'auto_tests_testsqldb'; however, it's still asking me about test_testsqldb.

Any advice is appreciated!

like image 437
Michael Merchant Avatar asked Jan 26 '11 20:01

Michael Merchant


People also ask

How do I run a specific test in Django?

You need to open you init.py and import your tests. Show activity on this post. If you want to run a test case class which has the path <module_name>/tests/test_views.py , you can run the command python manage.py test <module_name>. tests.

Which is default database in settings file in Django?

The name of database to use when running the test suite. If the default value ( None ) is used with the SQLite database engine, the tests will use a memory resident database. For all other database engines the test database will use the name 'test_' + DATABASE_NAME .

What is RequestFactory in Django?

The request factory The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.


1 Answers

In Django 1.6 and below, TEST_NAME should be a key of one of your database dictionaries. But in Django 1.7 and above, you use a TEST key which is a dictionary of settings for test databases.

You probably want:

DATABASES = {  'default':{    'ENGINE':'mysql',    'NAME':'testsqldb',    'USER':'<username>',    'PASSWORD':'<password>',    'TEST': {        'NAME': 'auto_tests',    }  },  'dynamic_data':{    'ENGINE': 'sqlite3',    'NAME':'',    'USER':'',    'PASSWORD':''  }, } 

Alternatively, perhaps you are wanting to use a different engine for your tests? In that case, I think you'll just have to create a separate settings file for testing. It can import from your standard settings module and override DATABASES.

like image 153
SmileyChris Avatar answered Oct 09 '22 01:10

SmileyChris