覆盖测试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/')
但是它不起作用,我的回溯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):
这是注销的 url :
url(r'^logout/$', views_authentication.logout_view, name = "logout"),
我认为函数根本没有被调用,但我不知道还能做什么...请帮忙??
提前致谢
首先,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
其中 --source = [所有应用]
这也可以在 .coveragerc
中配置
我在 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/')
但是它不起作用,我的回溯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):
这是注销的 url :
url(r'^logout/$', views_authentication.logout_view, name = "logout"),
我认为函数根本没有被调用,但我不知道还能做什么...请帮忙??
提前致谢
首先,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
其中 --source = [所有应用]
这也可以在 .coveragerc