zoukankan      html  css  js  c++  java
  • unittest中控制并孤立各种外部依赖

    一、外部依赖

    测试过程中经常伴随着某种外部依赖,列举如下:

    1. 可能需要从数据库加载、保存数据
    2. 可能依赖于某个特定的时间
    3. 可能从web上获取数据

    二、隔离方式

    2.1 数据源

    大多数实际的测试需要各种形式的数据,通常情况下,数据从数据库中读取。下面是一些控制数据源依赖关系的技术

    • 使用本地文件代替数据库
    • 使用内存中的数据库
    • 使用一个测试数据库

    2.2 资源虚拟化

    为控制系统之外资源的行为,可以虚拟化它们,即构造另一个版本的资源,新的版本可以模拟原有资源的API,而不需要内部实现。有如下技术:

    • 存根(stub)

       为测试期间的函数调用提供标准相应

    • 仿制(mock)

         仿制并代替实际对象的API

    • 伪造(fake)

       可以提供一个非常轻量级的实现

    三、仿制技术

    本文先讲解仿制(mock)技术,其他方式后续进行补充!

    什么是mock

    unittest.mock是一个用于在Python中进行单元测试的库,Mock翻译过来就是模拟的意思,顾名思义这个库的主要功能是模拟一些东西。
    它的主要功能是使用mock对象替代掉指定的Python对象,以达到模拟对象的行为。

    mock能做什么

    • 前后端联调,如果你是一个前端页面开发,现在需要开发一个功能

       下一个订单,支付页面的接口,根据支付结果,支付成功,展示支付成功页,支付失败,展示支付失败页。
       要完成此功能,你需要调用后端的接口,根据返回给你的结果,来展示不同的页面。此时后端接口还没开发好,
       作为一个前端开发总不能等别人开发好了,你再开发,那你只有加班的命了。
       为了同步开发完成任务,此时,你可以根据接口文档的规定,把接口的地址和入参传过去,然后自己mock接口的不同返回界面,来完成前端的开发任务

    • 单元测试

        单元测试的目的是测试某个小小单元的功能,但现实中开发的函数或方法都是有依赖关系的,比如b函数的参数,需要调用a函数的返回结果,但是我前面已经测试a函数了
        这种情况下,就不需要再测一次a函数了,此时就可以用mock模块来模拟调用这部分内容,并给出返回结果

    • 第三方接口依赖

        在做接口自动化的时候,有时候需要调用第三方的接口,但是别人公司的接口服务不受你的控制,有可能别人提供的测试环境今天服务给你开着,别人就关掉了,
        给自动化接口测试带来很多的麻烦,此时就可以自己写一个mock-server来模拟接口的返回数据

    mock环境准备

    • python2.x的版本,mock是一个独立的模块,需要用pip安装

        pip install -U mock

    • 从Python 3.3以后的版本mock已经合并到unittest模块中了,是unittest单元测试的一部分,直接导入过来就行

         from unittest import mock

    使用方式1

    datetimehelper.py代码如下:

    # Code Listing #3
    
    """ Module datetime helper - Contains the class DateTimeHelper providing
    some helpful methods for working with date and datetime objects """
    
    import datetime
    
    class DateTimeHelper(object):
        """ A class which provides some convenient date/time
        conversion and utility methods """
    
        def today(self):
            """ Return today's datetime """
            return datetime.datetime.now()
    
        def date(self):
            """ Return today's date in the form of DD/MM/YYYY """
            return self.today().strftime("%d/%m/%Y")
    
        def weekday(self):
            """ Return the full week day for today """
            return self.today().strftime("%A")
    
        def us_to_indian(self, date):
            """ Convert a U.S style date i.e mm/dd/yy to Indian style i.e dd/mm/yyyy """
    
            # Split it
            mm, dd, yy = date.split('/')
            yy = int(yy)
            # Check if year is >16, else add 2000 to it
            if yy <= 16:
                yy += 2000
            # Create a date object from it
            date_obj = datetime.date(year=yy, month=int(mm), day=int(dd))
            # Return it in correct format
            return date_obj.strftime("%d/%m/%Y")
    

    测试代码如下:

    # Code Listing #5
    
    """ Module test_datetimehelper -  Unit test module for testing datetimehelper module """
    
    # Note - This is the second version of test_datetimehelper module so named as test_datetimehelper2.py
    
    import unittest
    import datetime
    import datetimehelper
    
    from unittest.mock import patch
    
    
    class DateTimeHelperTestCase(unittest.TestCase):
        """ Unit-test testcase class for DateTimeHelper class """
        
        def setUp(self):
            self.obj = datetimehelper.DateTimeHelper()
    
        def test_date(self):
            """ Test date() method """
    
            # Put a specific date to test
            my_date = datetime.datetime(year=2016, month=8, day=16)
    
            # Patch the 'today' method with a specific return value
            with patch.object(self.obj, 'today', return_value=my_date):
                response = self.obj.date()
                self.assertEqual(response, '16/08/2016')
    
        def test_weekday(self):
            """ Test weekday() method """
    
            # Put a specific date to test
            my_date = datetime.datetime(year=2016, month=8, day=21)
    
            # Patch the 'today' method with a specific return value
            with patch.object(self.obj, 'today', return_value=my_date):
                response = self.obj.weekday()
                self.assertEqual(response, 'Sunday')            
                              
        def test_us_india_conversion(self):
            """ Test us=>india date format conversion """
    
            # Test a few dates
            d1 = '08/12/16'
            d2 = '07/11/2014'
            d3 = '04/29/00'
            self.assertEqual(self.obj.us_to_indian(d1), '12/08/2016')
            self.assertEqual(self.obj.us_to_indian(d2), '11/07/2014')
            self.assertEqual(self.obj.us_to_indian(d3), '29/04/2000')
    
    
    if __name__ == "__main__":
        unittest.main()
    

      

    使用方式2

    # 保存为temple.py
    
    # coding:utf-8
    # 作者:上海-悠悠 QQ交流群:588402570
    
    def zhifu():
        '''假设这里是一个支付的功能,未开发完
        支付成功返回:{"result": "success", "reason":"null"}
        支付失败返回:{"result": "fail", "reason":"余额不足"}
        reason返回失败原因
        '''
        pass
    
    def zhifu_statues():
        '''根据支付的结果success or fail,判断跳转到对应页面'''
        result = zhifu()
        print(result)
        try:
            if result["result"] == "success":
                return "支付成功"
            elif result["result"] == "fail":
                print("失败原因:%s" % result["reason"])
                return "支付失败"
            else:
                return "未知错误异常"
        except:
            return "Error, 服务端返回异常!"
    

    单元测试用例设计

    # coding:utf-8
    from unittest import mock
    import unittest
    import temple
    # 作者:上海-悠悠 QQ交流群:588402570
    
    class Test_zhifu_statues(unittest.TestCase):
        '''单元测试用例'''
        def test_01(self):
            '''测试支付成功场景'''
            # mock一个支付成功的数据
            temple.zhifu = mock.Mock(return_value={"result": "success", "reason":"null"})
            # 根据支付结果测试页面跳转
            statues = temple.zhifu_statues()
            print(statues)
            self.assertEqual(statues, "支付成功")
    
        def test_02(self):
            '''测试支付失败场景'''
            # mock一个支付成功的数据
            temple.zhifu = mock.Mock(return_value={"result": "fail", "reason": "余额不足"})
            # 根据支付结果测试页面跳转
            statues = temple.zhifu_statues()
            self.assertEqual(statues, "支付失败")
    
    if __name__ == "__main__":
        unittest.main()
    

      

    参考文档:https://www.cnblogs.com/yoyoketang/p/9346660.html  

  • 相关阅读:
    LabelImg 图像图像标注工具
    周杰伦的2000w个故事
    ROS 订阅图像节点(1)
    ROS 订阅图像节点
    ROS 双目标定
    书籍
    Z30云台PC控制问题
    大疆M600组装和试飞
    M100 X3云台安装
    M100 组装教程
  • 原文地址:https://www.cnblogs.com/hester/p/12121617.html
Copyright © 2011-2022 走看看