我使用django.test.client.Client来测试当用户登录时是否显示一些文本。但是,客户端对象似乎并没有让我保持登录状态。
如果使用Firefox手动完成此测试,但在使用Client对象时不会通过此测试。
class Test(TestCase):
def test_view(self):
user.set_password(password)
user.save()
client = self.client
# I thought a more manual way would work, but no luck
# client.post('/login', {'username':user.username, 'password':password})
login_successful = client.login(username=user.username, password=password)
# this assert passes
self.assertTrue(login_successful)
response = client.get("/path", follow=True)
#whether follow=True or not doesn't seem to work
self.assertContains(response, "needle" )当我打印响应时,它返回被以下内容隐藏的登录表单:
{% if not request.user.is_authenticated %}
... form ...
{% endif %}这在我运行ipython manage.py shell时得到了确认。
问题似乎是客户端对象没有保持会话的身份验证。
发布于 2010-05-21 21:05:55
FWIW,Django 1.2的更新(我之前运行的是1.1.1 )修复了这个问题。我不知道那里出了什么问题,考虑到我上一次运行测试套件是在大约两周前,它工作得很好。
https://stackoverflow.com/questions/2878602
复制相似问题