0

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

我不知道从那里去哪里。

4

2 回答 2

1

问题与看起来完全不同。在我的测试中,我写道:

    self.assertTrue(self.client.login(user='chris', password='password'),
        'Unexpected login failure.')

当关键字 arg 应该改为username. 我遵循了数据库中丢失数据的红鲱鱼,这是由transactions解释的。

感谢 #django 中的 mlvin 的帮助。

于 2012-08-24T20:29:39.357 回答
0

我想它与测试数据库有关(正如您最初的直觉所暗示的那样)。如果您查看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.argvtest修改您的数据库设置,从而忽略了固定装置。你grep能把你的项目test放在意想不到的地方吗?我有一个settings.py这样的部分:

if 'test' in sys.argv:                              
    # Switch to sqlite for tests (it's *way* faster)                              
    DATABASE_ENGINE = 'sqlite3'                  
于 2012-08-23T22:43:00.213 回答