Django 1.3 版。
我是 Django 单元测试的新手。我已经编写了我的第一个测试,但它失败了,因为我的登录不起作用。我很确定这样做的原因是数据库不包含我应该由auth
夹具提供的用户帐户。
我写的代码是:
from django.test import TestCase
from django.test.client import Client
class OrderCreateTest(TestCase):
fixtures = ['auth']
def setUp(self):
self.client = Client()
def testSelectProduct(self):
self.assertTrue(self.client.login(user='chris', password='password'),
'Unexpected login failure.')
response = self.client.get('/order/new/')
print response
self.client.logout()
def tearDown(self):
pass
def runTest(self):
pass
我得到的测试失败是:
Creating test database for alias 'default' ('test_proj')...
Creating tables ...
...
Running post-sync handlers for application admin
...
Installing custom SQL ...
Installing indexes ...
Installing index for admin.LogEntry model
...
Running post-sync handlers for application admin
...
Loading 'initial_data' fixtures...
Checking '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/fixtures' for fixtures...
No xml fixture 'initial_data' in '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/fixtures'.
...
Checking absolute path for fixtures...
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No xml fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No json fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No yaml fixture 'initial_data' in absolute path.
No fixtures found.
Destroying test database for alias 'default' ('test_proj')...
testSelectProduct (orpheus.orders.tests.ordercreate.OrderCreateTest) ... FAIL
======================================================================
FAIL: testSelectProduct (orpheus.orders.tests.ordercreate.OrderCreateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/path/to/ordercreate.py", line 24, in testSelectProduct
'Unexpected login failure.')
AssertionError: Unexpected login failure.
----------------------------------------------------------------------
Ran 1 test in 2376.829s
FAILED (failures=1)
Destroying test database for alias 'default' ('test_proj')...
从我的数据库设置片段:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'project',
'USER': 'project',
'PASSWORD': 'projectdb',
'TEST_NAME': 'test_project' } }
我已经逐步完成了测试代码,我可以看到它正在找到我的夹具auth
并且它正在保存它找到的数据......某处。我已暂停测试以放入 mysql 并查询相关表,但它们都保持为空(测试代码已跳过处理事务的部分,因此我确信我不应该等待提交)。
我在做什么或不做什么使我的固定装置无法正确加载?
编辑:我有一个绝妙的主意来测试加载夹具并且它成功了:
$ ./manage.py loaddata auth
Installed 8 object(s) from 1 fixture(s)
但是,这并没有给我任何关于如何修复夹具加载的想法。
编辑:当他建议我尝试这个时,supervacuo 提供了一个线索:
./manage.py testserver -v 2 auth.json
输出基本相同,只是它正确加载了fixture(mysql确认了它):
...
Installed 8 object(s) from 1 fixture(s)
Validating models...
0 errors found
Django version 1.3, using settings 'project.settings'
Running django-devserver 0.1.4
我不知道从那里去哪里。