【发布时间】:2017-02-22 16:33:41
【问题描述】:
我正在学习python
我想知道是否有一种机制可以将一个对象(在我的例子中是一个假对象)“注入”到正在测试的类中,而无需在 costructor/setter 中显式添加它。
## source file
class MyBusinessClass():
def __init__(self):
self.__engine = RepperEngine()
def doSomething(self):
## bla bla ...
success
## test file
## fake I'd like to inkject
class MyBusinessClassFake():
def __init__(self):
pass
def myPrint(self) :
print ("Hello from Mock !!!!")
class Test(unittest.TestCase):
## is there an automatic mechanism to inject MyBusinessClassFake
## into MyBusinessClass without costructor/setter?
def test_XXXXX_whenYYYYYY(self):
unit = MyBusinessClass()
unit.doSomething()
self.assertTrue(.....)
在我的测试中,我想“注入”对象“引擎”而不将其传递给构造函数。我尝试了几个选项(例如:@patch ...)但没有成功。
【问题讨论】:
标签: python mocking python-unittest