当从管理命令运行时,Django-nose测试用例会出现断言错误

时间:2013-04-19 17:54:14

标签: python django unit-testing testing nose

test.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
    >>> 

用户&#39;测试&#39;在django活跃当前的场景。

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

2 个答案:

答案 0 :(得分:2)

看起来它没有继承基础测试类,因此它不会在测试之前调用setup方法。我建议继续使用Django的TestCase类,按照the Django documentation on 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)

答案 1 :(得分: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