我需要测试类的构造函数是否调用了某些方法
class ProductionClass:
def __init__(self):
self.something(1, 2, 3)
def method(self):
self.something(1, 2, 3)
def something(self, a, b, c):
pass
此类来自“unittest.mock-getting started”。正如上面写的,我可以确保“method”调用“something”如下。
real = ProductionClass()
real.something = MagicMock()
real.method()
real.something.assert_called_once_with(1, 2, 3)
但是如何对构造器进行同样的测试呢?
您可以使用patch(检查文档中的https://docs.python.org/dev/library/unittest.mock.html)并断言,在创建对象的新实例之后,
something
方法被调用一次,并使用所需的参数调用。例如,在您的示例中,可能是这样的:相关问题 更多 >
编程相关推荐