Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
712 views
in Technique[技术] by (71.8m points)

testing - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...