无法使用django-allauth中的单元测试登录

时间:2015-01-08 13:18:34

标签: python django unit-testing django-views django-allauth

我试图用这样的单元测试来测试我的观点

class TestClassroomView(TestCase):

def test_classroom_view(self):
    auth_headers = {'HTTP_AUTHORIZATION': 'Basic ' + base64.b64encode('test@tst.com:test')}
    response = self.client.get('/classroom/3/', follow=True, **auth_headers)
    self.assertContains(response, 'Course progress')
    self.assertEqual(response.status_code, 200)

而且:

class TestClassroomView(TestCase):
 def test_classroom(self):
    self.client.login(username='test@tst.com', password='test')
    response = self.client.get('/classroom/3/', follow=True)
    self.assertEqual(response.status_code, 200)
    self.assertTemplateUsed(response, 'web/dashboard.html')

response.redirect_chain始终显示此

[('http://testserver/?next=/classroom/3/', 302)]

如果我只留下status_code个支票,那么测试通过,但实际上他们没有测试任何东西。 我正在使用django-allauth身份验证系统 请帮我弄清楚我做错了什么。

另外如何在测试开始时加载灯具?

1 个答案:

答案 0 :(得分:1)

这就是帮助我进行身份验证的原因

from django.contrib.auth import get_user_model 

class TestParent(TestCase):

  def setUp(self):
    username = 'testuser'
    password = 'testpass'
    User = get_user_model()
    user = User.objects.create_user(username, password=password)
    logged_in = self.client.login(username=username, password=password)
    self.assertTrue(logged_in)