开发者社区> 问答> 正文

适用于属性的python存根框架

有谁知道一个mocking-或磕碰的框架,允许磕碰性能甚至classproperties这样的装饰呢?

class classproperty:
    """
    Decorator to  read-only static properties
    """
    def __init__(self, getter):
        self.getter = getter
    def __get__(self, instance, owner):
        return self.getter(owner)

class Foo:
    _name = "Name"
    @classproperty
    def foo(cls):
        return cls._name

我当前正在使用嘲笑,但这不允许对属性进行存根。

展开
收起
祖安文状元 2020-02-23 16:00:37 721 0
1 条回答
写回答
取消 提交回答
  • 使用unittest.mock.PropertyMock(自Python 3.3起可用):

    from unittest import mock
    with mock.patch.object(Foo, 'foo', new_callable=mock.PropertyMock) as m:
        m.return_value = 'nAME'
        assert Foo.foo == 'nAME'
    
    

    注意:如果您使用的Python版本低于3.3,请使用mock。

    2020-02-23 16:00:45
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载