您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在python单元测试中模拟类属性的更好方法

在python单元测试中模拟类属性的更好方法

base.Base.assignment简单地用一个Mock对象代替。您 提出 通过添加它描述__get__方法

这有点冗长,有些不必要。您可以base.Base.assignment直接直接设置:

def test_empty(self):
    Base.assignment = {}
    assert len(Base().assignment.values()) == 0

当然,在使用测试并发时,这不太安全。

要使用PropertyMock,我将使用:

with patch('base.Base.assignment', new_callable=PropertyMock) as a:
    a.return_value = {'a': 1}

甚至:

with patch('base.Base.assignment', new_callable=PropertyMock, 
           return_value={'a': 1}):
python 2022/1/1 18:42:17 有282人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶