【问题标题】:Django-nose test case gives assertion error when run from management command从管理命令运行时,Django-nose 测试用例给出断言错误
【发布时间】:2013-04-19 17:54:14
【问题描述】:

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

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

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

【问题讨论】:

  • 人们想通了,请参阅:stackoverflow.com/questions/16116776/…>

标签: python django unit-testing testing nose


【解决方案1】:

看起来它没有继承基础测试类,因此它没有在您的测试之前调用 setup 方法。根据the Django documentation on testing,我建议从 Django 的 TestCase 类继承。在这种情况下,它看起来像这样:

# 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)

【讨论】:

  • @d03514 可以根据您的解决方案尝试运行测试并共享输出
  • 我想你运行起来会更容易,我的本地数据库中没有设置名为 test 的用户。
【解决方案2】:

您是否正在为您的测试创建用户名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。

【讨论】:

  • 那么如何默认加载一个测试用户..这是测试所有具有 user.is_authenticated() 的视图的实际意图
  • @cool_n_curious 我通常在 setUp 方法中创建一个测试用户,这样您就可以在需要针对经过身份验证的用户进行测试的任何视图中登录该用户
  • 不起作用!在我问之前尝试过..这是我所做的..在全球范围内:from django.contrib.auth.models import User修改设置:def setup(self):#noz.set_trace()self.client = Client()# setup a test userusr = User(username='test', password='test')usr.save()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-15
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
相关资源
最近更新 更多