【发布时间】:2018-09-21 22:22:00
【问题描述】:
为了测试,我想在基类中更改单个 Class 实例的属性 (self.attr)。
# app.py
class Base():
def __init__(self):
self.attr = 'original_value'
def show(self):
print(self.attr)
class App():
def __init__(self):
self.base = Base()
这是我尝试模拟 Base 类实例的属性 attr
# test_app.py
from mock import Mock
from app import App
def test_mock_inherited_class_instance():
""" With mocking. Change app.base.attr from 'original_value' to 'new_value'.
"""
app = App()
app.base = Mock()
app.base.attr = 'new_value'
app.base.show() # I'd like this to show 'new_value', NOT 'original_value'
【问题讨论】:
标签: python unit-testing mocking patch