Search code examples
pythondjangocoverage.pytest-coverage

Coverage test django logout


I got the django logout function inside my views.py :

def logout_view(request):

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

And im trying to test it using coverage with this code:

class TestLogout(TestCase):

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

But it's not working, my traceback returns none :

> /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):

This is the url for logout :

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

I think function it's not beign called at all but i don't know what else to do...any help please ??

Thanks in advance


Solution

  • First, It appears that there is a problem to the url. I think it should be

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

    Also, I suggest to do login the user first. So,

    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)
    

    For running coverage, you can do: coverage run --source=.,cv_manage manage.py test where --source = [all apps] this can also be configured in .coveragerc