【问题标题】:Coverage test django logout覆盖测试django注销
【发布时间】:2016-02-15 13:54:38
【问题描述】:

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

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

提前致谢

【问题讨论】:

  • 你能在没有 pdb 和完整的 TestLogout 类的情况下显示回溯吗?有setUp方法创建的用户吗?
  • 您好,感谢您的回答。没有 pdb 的 Mmm 不显示任何错误,覆盖继续并完成,但将 logout_view 标记为未测试......这是 TestLogout 完成,那里没有更多代码,是的,我在夹具数据中创建了一个用户,但我不知道会发生什么开。
  • 你能显示关于注销的 urls.py 以及它是如何匹配到 logout_view 的吗?
  • 当然,看:url(r'^logout/$', views_authentication.logout_view, name = "logout"),在我的问题中也更新了。
  • 如果 response 不是 None,你能打印 response.status,还是在 logout_view 中使用 pdb 断点?如果不是,我们需要确保此测试将退出 logout_view,因此问题将出现在 /logout/

标签: python django coverage.py test-coverage


【解决方案1】:

首先,网址似乎有问题。我觉得应该是

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中配置

【讨论】:

  • 做得很完美!!非常感谢您的帮助!!
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 2014-02-26
  • 2012-03-25
  • 1970-01-01
  • 2020-05-25
  • 2019-05-09
  • 2018-10-20
  • 1970-01-01
相关资源
最近更新 更多