用于鼻子测试的Django用户设置 [英] Django user setup for nose tests

查看:61
本文介绍了用于鼻子测试的Django用户设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置一个用于进行鼻子测试的用户,但这不起作用

I tried seting up a user for nose tests but it doesnot work

在已定义的全局范围内:

in global scope defined:

from django.contrib.auth.models import User
import nose.tools as noz

在定义的测试类中:

def setUp(self):
    self.client = Client()
    user = User(username="test", password="test")
    user.save()

用户已保存,我已经使用noz.set_trace()测试过 但是当测试功能要求使用同一用户的登录名时,会引发断言错误:

The user is saved, which i have tested with the noz.set_trace() but when a test function calls for the same user's login, assertion error is raised:

nosetests --verbosity 1
Creating test database for alias 'default'...
> <app-path>/tests.py(59)testTestUser()
-> response = self.client.login(username=u'test', password=u'test')
(Pdb) User.objects.all()
[<User: test>]

testTestUser函数的定义如下:

the testTestUser function is defined like:

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

相关测试输出为:

    noz.assert_equal(response, True)
       AssertionError: False != True

    ----------------------------------------------------------------------
    Ran 1 test in 0.011s

    FAILED (failures=1)

我的目的是测试具有requst.user.is_authenicated()分支的视图.

My intention is to test views that have requst.user.is_authenicated() branch.

推荐答案

从以下位置将其弄清楚: http://www. pukkared.com/2011/07/simulating-user-login-in-a-django-view-unit-test/

figured it out from : http://www.pukkared.com/2011/07/simulating-user-login-in-a-django-view-unit-test/

正确的代码是:

def setUp(self):
    self.client = Client()
    user = User.objects.create_user(username="test", password="test")

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

这篇关于用于鼻子测试的Django用户设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆