【问题标题】:how to write get and post test methods for views in django?如何在 django 中为视图编写获取和发布测试方法?
【发布时间】:2017-11-24 11:21:18
【问题描述】:

我是 Django 测试的新手,正在尝试为视图编写测试函数,但我不了解整个测试视图的内容,我看过很多示例,但似乎很难,我需要一个我写的东西的例子来得到这个想法。 这是一个我想测试它的帖子并得到的函数:

    def ForgetPasswordRegistry(self, request):
    forgetpassword = True
    if request.method == 'GET':
        if 'UserID' in request.session:
            forgetpassword = False
        return request, forgetpassword
    elif request.method == 'POST':
        email = request.POST['email']
        if self.IsUserExist(email):
            forget = models.ForgetPasswordRegistry()
            forget.UserID = self.GetUser(email)
            forget.Access = 1
            session = random.randint(999999999999999, 9999999999999999999999999999999999)
            forget.Session = str(session)
            link = 'http://127.0.0.1:8000/home/resetpassword/' + forget.Session
            self.SendEmail('Reset-Password', link, [forget.UserID.Email])
            forget.save()
            FeedBack = ' Check Your Mail You Got A Recovery Mail'
            AlertType = 'alert-success'
            return request, AlertType, None, FeedBack, None, None
        else:
            AlertType = 'alert-danger'
            ModalFeedBack = 'This Email Dose Not Exist'
            EmailErrors = 'This Email Dose Not Exist'
            return request, AlertType, forgetpassword, None, EmailErrors, ModalFeedBack

【问题讨论】:

    标签: python django unit-testing django-views django-testing


    【解决方案1】:

    不要让视图的概念迷惑你!!本质上,有一个 Python 函数需要您执行某些条件。

    看起来有4个主要分支:

    • GET 不在会话中
    • 在会话中获取
    • POST 存在
    • POST 不存在

    所以应该至少有 4 种不同的测试。我将发布第一个,因为它非常简单:

    def ForgetPasswordRegistry(self, request):
    forgetpassword = True
    if request.method == 'GET':
        if 'UserID' in request.session:
            forgetpassword = False
        return request, forgetpassword
    
    def test_forget_get_user_id_in_session(self):
        request = Mock(session={'UserID': 'here'}, method='GET')
        # instantiate your view class
        yourview = YourView()
        sefl.assertEqual(yourview.ForgetPasswordRegistry(request), (request, False))
    

    post user exists 分支稍微复杂一些,因为测试需要考虑更多的事情,并可能为以下内容提供存根实现:

    • 发送电子邮件
    • 获取用户
    • models.ForgetPasswordRegistry()

    【讨论】:

      【解决方案2】:

      使用 Django 的测试客户端:https://docs.djangoproject.com/en/stable/topics/testing/tools/#the-test-client

      例子:

      from django.test import Client, TestCase
      
      
      class ViewTestCase(TestCase):
          def test_post_creation(self):
              c = Client()  # instantiate the Django test client
              response = c.get('/forgetpassword/')
              self.assertEqual(response.status, 200)
      

      【讨论】:

      • 这看起来不错,但我想检查 post 和 get 是否有效。
      • 不确定你的意思,状态码 200 表示 HTTP 响应有效。如果还有其他要检查的内容(例如“成功”的文本),您可以检查响应的正文(例如通过response.body
      • TestCase 有一个 attr self.client
      猜你喜欢
      • 2018-05-10
      • 2014-07-14
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多