我正在使用django.test.client.Client来测试当用户登录时是否显示一些文本。但是,我的客户端对象不会' t似乎让我登录。为什么django.test.client.Client不让我登录
该测试通过,如果手动完成与Firefox,但不完成与客户端对象。
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
这证实。
问题似乎是客户端对象没有保持会话认证。
看着这个我不确定问题在哪里。你能明确表明客户端正在注销吗?如果客户端已经登录,那么'client.session!= {}'您可以使用它来显示客户端在何处登出。 – tdedecko 2010-05-21 04:13:42
这肯定没有帮助你,对不起,但我从今天起就有同样的问题。我不知道改变了什么。 – Fred 2010-05-21 12:47:13