2

我在 views.py 中获得了 django 注销功能:

def logout_view(request):

    logout(request) 
    return HttpResponseRedirect(reverse('cost_control_app:login'))

我尝试使用以下代码的覆盖率对其进行测试:

class TestLogout(TestCase):

   def test_logout(self):
        self.client = Client()
        response = self.client.get('/logout/')

但它不起作用,我的回溯没有返回:

> /home/juanda/cost_control_repository/cost_control/cost_control_app/unitary_test/test_views_authentication.py(73)TestLogout()
-> def test_logout(self):
(Pdb) n
--Return--
> /home/juanda/cost_control_repository/cost_control/cost_control_app/unitary_test/test_views_authentication.py(73)TestLogout()->None
-> def test_logout(self):

这是注销的网址:

url(r'^logout/$', views_authentication.logout_view, name = "logout"),

我认为函数根本没有被调用,但我不知道还能做什么......请帮助?

提前致谢

4

1 回答 1

3

首先,似乎url有问题。我认为应该是

class TestLogout(TestCase):

   def test_logout(self):
        self.client = Client()
        response = self.client.get('/cost_control/logout/')

另外,我建议先登录用户。所以,

class TestLogout(TestCase):

   def test_logout(self):
        self.client = Client()
        # Assuming there is a user exists in tests db
        # or make a user like.
        # User.objects.create_user(username='fred', email='test@test.com', password='secret') 
        self.client.login(username='fred', password='secret')
        response = self.client.get('/cost_control/logout/')
        self.assertEqual(response.status_code, 302)

对于运行覆盖,您可以执行以下操作: coverage run --source=.,cv_manage manage.py test where --source = [all apps] 这也可以在 .coveragerc 中配置

于 2016-02-15T16:38:03.373 回答