1

测试.py

# write code to test the views.
from django.test import Client

# import nose for tests.
import nose.tools as noz

class TestSettings(object):
    """ test the nose test related setup """

    def setup(self):
        self.client = Client()

    def testTestUser(self):
        """ Tests if the django user 'test' is setup properly."""
        # login the test user
        response = self.client.login(username=u'test', password=u'test')    
        noz.assert_equal(response, True)

从管理命令运行此代码时,会给出以下输出:

    $ ./manage.py test <app-name>
    nosetests --verbosity 1 <app-name>
    Creating test database for alias 'default'...
    F
    ======================================================================
    FAIL: Tests if the django user 'test' is setup properly.
    ----------------------------------------------------------------------
    Traceback (most recent call last):
       File "/<python-sitepackages-dir-path>/nose/case.py",                                                                                       line 197, in runTest
       self.test(*self.arg)
       File "<application-path>/tests.py", line 28, in testTestUser
       noz.assert_equal(response, True)
    AssertionError: False != True

    ----------------------------------------------------------------------

    Ran 1 test in 0.008s

    FAILED (failures=1)
    Destroying test database for alias 'default'...

现在通过 django shell 运行时相同的命令给出以下内容:

    $ ./manage.py shell
    Python 2.6.6 (r266:84292, Sep 11 2012, 08:28:27) 
    [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from django.test import Client
    >>> 
    >>> import nose.tools as noz
    >>> 
    >>> client = Client()
    >>> response = client.login(username=u'test', password=u'test')
    >>> noz.assert_equal(response, True)
    >>> 
    >>> 
    >>> response
    True
    >>> 

用户 'test' 在 django 中对于当前场景处于活动状态。

为什么我在运行管理命令时收到此错误断言?

4

2 回答 2

2

看起来它没有继承基础测试类,所以它没有在测试之前调用 setup 方法。我会建议从 Django 的 TestCase 类继承,根据Django 文档的 testing。在这种情况下,它看起来像这样:

# write code to test the views.
from django.test import Client
import unittest

# import nose for tests.
import nose.tools as noz

class TestSettings(unittest.TestCase):
    """ test the nose test related setup """

    def setUp(self):  # Note that the unittest requires this to be setUp and not setup
        self.client = Client()

    def testTestUser(self):
        """ Tests if the django user 'test' is setup properly."""
        # login the test user
        response = self.client.login(username=u'test', password=u'test')    
        noz.assert_equal(response, True)
于 2013-04-19T18:00:25.647 回答
0

您是否正在为您的测试创建具有用户名test和密码的用户test?还是加载夹具?我打赌不会。

当您使用 shell 时,您正在登录 settings.py 中的数据库。当您进行测试时,您正在使用您的测试数据库,该数据库在每次测试开始时都是空的,因此没有用户。

setUp你可以创建一个用户

from django.contrib.auth.models import User
User.objects.create('test', 'test@test.com', 'test')

正如@Kevin London 所指出的那样

你的设置命令应该是

setUp,但我认为这与它没有太大关系,因为默认情况下每个TestCase都有一个client

于 2013-04-19T17:59:28.340 回答