Django 1.3版。
我是Django单元测试的新手。我已经编写了我的第一个测试但它失败了,因为我的登录不起作用。我很确定这样做的原因是数据库不包含我应该由auth
fixture提供的用户帐户。
我写的代码是:
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')...
我使用db:
设置的片段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
输出大致相同,只是它正确加载了灯具(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
我不知道从哪里去。
答案 0 :(得分:1)
问题与它看起来完全不同。在我的测试中,我写道:
self.assertTrue(self.client.login(user='chris', password='password'),
'Unexpected login failure.')
当关键字arg应改为username
时。我跟踪了数据库中缺少数据的红色鲱鱼,由transactions解释。
感谢#django的mlavin提供的帮助。
答案 1 :(得分:0)
我想它与测试数据库有关(正如你最初的本能所暗示的那样)。如果您查看the relevant code from Django,它只会在内部运行loaddata
:
# django/test/testcases.py
if hasattr(self, 'fixtures'):
# We have to use this slightly awkward syntax due to the fact
# that we're using *args and **kwargs together.
call_command('loaddata', *self.fixtures,
**{'verbosity': 0, 'database': db})
因此,如果loaddata
在测试之外工作(就像它显然那样),那么问题必然在于其他地方。鉴于它与./manage.py testserver
一起使用,我怀疑代码库中的某些内容正在检查sys.argv
test
并修改数据库设置,以便忽略fixture。您可以在grep
项目中test
在意外的地方settings.py
吗?我在if 'test' in sys.argv:
# Switch to sqlite for tests (it's *way* faster)
DATABASE_ENGINE = 'sqlite3'
中有一个这样的部分:
{{1}}