我试图逐个单独测试我的Django测试文件,因为重度应用程序每次运行后./manage.py test
冻结5秒。
这是我的测试(尽管不是测试,只是玩请求):
if __name__ == "__main__":
import unittest
# manually get all the django stuff into memory if file is called directly
import os,sys
TEST_ROOT = os.path.realpath(os.path.dirname(__file__))
PROJ_AND_TEST_ROOT = os.path.dirname(os.path.dirname(TEST_ROOT))
PROJECT_ROOT = os.path.join(PROJ_AND_TEST_ROOT, 'the_game')
sys.path.append(PROJECT_ROOT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "the_game.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
from django.test import TestCase, RequestFactory
from django.contrib.auth.models import User
from interface.views import bets
class ViewTests(TestCase):
def test_bets_view(self):
request_factory=RequestFactory()
request=request_factory.get('/')
user = User.objects.create_user('testname','test@na.me','testname')
request.user=user
response=bets(request)
print response
if __name__ == "__main__":
unittest.main()
然而,运行这个我得到以下内容:
======================================================================
ERROR: test_bets_view (__main__.ViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
self._pre_setup()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 754, in _pre_setup
self._fixture_setup()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 887, in _fixture_setup
if not connections_support_transactions():
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 874, in connections_support_transactions
for conn in connections.all())
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/test/testcases.py", line 874, in <genexpr>
for conn in connections.all())
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/utils/functional.py", line 55, in __get__
res = instance.__dict__[self.func.__name__] = self.func(instance)
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 782, in supports_transactions
self.connection.leave_transaction_management()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 338, in leave_transaction_management
if managed == self.get_autocommit():
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 345, in get_autocommit
self.ensure_connection()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/Users/1111/.virtualenvs/the_game/lib/python2.7/site-packages/django/db/utils.py", line 86, in __exit__
db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
AttributeError: 'DatabaseWrapper' object has no attribute 'Database'
项目本身完美无误(./manage.py runserver
),django测试(./manage.py test ../tests
)也是如此。
我该如何解决这个问题?
P.S。这不是this question的重复。它的作者在使用标准Django测试时遇到了问题,而它对我的项目工作正常。我的麻烦在于第三方测试。
答案 0 :(得分:0)
不幸的是,你所做的并不足以初始化django的设置系统。你可以试试这个:
from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
或者你可以考虑编写一个自定义管理命令(它已经初始化了设置系统),它会调用测试运行器。