我想在Python中进行一个简单的测试,但我不知道如何完成mocking过程。
这是类和def代码:
class FileRemoveOp(...)
@apply_defaults
def __init__(
self,
source_conn_keys,
source_conn_id='conn_default',
*args, **kwargs):
super(v4FileRemoveOperator, self).__init__(*args, **kwargs)
self.source_conn_keys = source_conn_keys
self.source_conn_id = source_conn_id
def execute (self, context)
source_conn = Connection(conn_id)
try:
for source_conn_key in self.source_keys:
if not source_conn.check_for_key(source_conn_key):
logging.info("The source key does not exist")
source_conn.remove_file(source_conn_key,'')
finally:
logging.info("Remove operation successful.")
这是我对「execute」函数的测试:
@mock.patch('main.Connection')
def test_remove_execute(self,MockConn):
mock_coon = MockConn.return_value
mock_coon.value = #I'm not sure what to put here#
remove_operator = FileRemoveOp(...)
remove_operator.execute(self)
由于execute方法尝试建立连接,我需要模拟它,我不想建立真正的连接,只需返回一些模拟数据。我如何做到这一点?我习惯在Java中进行测试,但我从未在Python中进行过。