Mock源码阅读

栏目: Python · 发布时间: 6年前

内容简介:写单元测试的时候,经常要借助Mock来完成某些替换,鉴于Python的动态性,Python中 写单元测试还是很方便的。但是需要吐槽的是,也是因为Python的动态性,Python的功能 必须要由高覆盖率的测试来保证。看完mock的代码感觉有两点:

写单元测试的时候,经常要借助Mock来完成某些替换,鉴于 Python 的动态性,Python中 写单元测试还是很方便的。但是需要吐槽的是,也是因为Python的动态性,Python的功能 必须要由高覆盖率的测试来保证。

Mock源码以及注释

https://github.com/jiajunhuang/cpython/blob/annotation/Lib/unittest/mock.py

感受

看完mock的代码感觉有两点:

  • 标准库的代码并不是那么“标准” --- 并不怎么遵守pep8

  • 标准库的代码测试覆盖率超高

看完mock的代码以后给CPython提交了两个patch,一个是优化了一下写法,一个是修复了 一个bug:

造个小轮子

简单实现一个mock如下:

class Mock:
    return_value = NotImplemented

    def __new__(cls, *args, **kwargs):
        new = type(cls.__name__, (cls, ), {'__doc__': cls.__doc__})
        return object.__new__(new)

    def __init__(self, return_value):
        self.return_value = return_value

    def __call__(self):
        return self.return_value if self.return_value else None


class PatchObject:
    def __init__(self, instance, attr_name, value):
        self.attr_origin = getattr(instance, attr_name, None)
        self.attr_name = attr_name
        self.instance = instance
        self.value = value

    def __enter__(self):
        """perform the patch"""
        mocked = Mock(self.value)
        setattr(self.instance, self.attr_name, mocked)
        return mocked

    def __exit__(self, exc_type, exc_val, exc_rb):
        """unpatch"""
        if self.attr_origin:
            setattr(self.instance, self.attr_name, self.attr_origin)
        else:
            delattr(self.instance, self.attr_name)


if __name__ == "__main__":
    # test mock
    class Foo:
        def foo(self):
            pass

    foo = Foo()
    foo.foo = Mock(1)

    bar = Foo()
    bar.foo = Mock(2)

    print(foo.foo(), bar.foo())

    # test patch object
    baz = Foo()
    with PatchObject(baz, "baz", 3) as mocked_baz:
        print(mocked_baz())
    print(dir(baz))

运行结果:

(1, 2)
3
['__doc__', '__module__', 'foo']

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Data Structures and Algorithms

Data Structures and Algorithms

Alfred V. Aho、Jeffrey D. Ullman、John E. Hopcroft / Addison Wesley / 1983-1-11 / USD 74.20

The authors' treatment of data structures in Data Structures and Algorithms is unified by an informal notion of "abstract data types," allowing readers to compare different implementations of the same......一起来看看 《Data Structures and Algorithms》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具