簡體   English   中英

覆蓋率測試Django注銷

[英]Coverage test django logout

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

我認為函數根本不是良性調用,但是我不知道還有什么要做的...請幫忙嗎?

提前致謝

首先,看來網址有問題。 我認為應該

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,您可以執行以下操作: coverage run --source=.,cv_manage manage.py test ,其中--source = [所有應用]也可以在.coveragerc中配置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM