覆盖测试django注销

时间:2016-02-15 13:54:38

标签: python django coverage.py test-coverage

我在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"),

我认为功能根本不是所谓的,但我不知道还能做什么...有什么帮助吗?

提前致谢

1 个答案:

答案 0 :(得分:2)

首先,网址似乎存在问题。我认为应该是

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 = [所有应用] 这也可以在.coveragerc

中配置