我已经从Django Docs中读过TestCase模块,我试图在我的django项目中使用它。但是我得到了这样的错误:
**
Error
Traceback (most recent call last):
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/test/testcases.py", line 1026, in setUpClass
if not connections_support_transactions():
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/test/testcases.py", line 991, in connections_support_transactions
for conn in connections.all())
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/test/testcases.py", line 991, in <genexpr>
for conn in connections.all())
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/db/backends/base/features.py", line 226, in supports_transactions
cursor.execute('CREATE TABLE ROLLBACK_TEST (X INT)')
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/hp/.virtualenvs/shouchela/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1142, "CREATE command denied to user 'xxxx'@'xxx' for table 'ROLLBACK_TEST'")
**
这是我的代码:
import os
import socket
import sys
hostname = socket.gethostname()
SETTINGS_DICT = {
'slave': "shouchela.settings.chewantest",
'master': "shouchela.settings.dev",
}
SETTINGS = SETTINGS_DICT.get(hostname, "shouchela.settings.dev")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", SETTINGS)
parent = lambda path, count: parent(os.path.dirname(path), count - 1) if count > 0 else path
sys.path.insert(0, parent(os.path.abspath(__file__), 2))
reload(sys)
sys.setdefaultencoding('utf-8')
import django
django.setup()
from django.test import TestCase
class WebOpsTest(TestCase):
def test_search(self):
response = self.client.get('/ops/car/search/')
print response
有人能告诉我我的代码有什么问题吗?怎么解决?非常感谢。