小编Ana*_*naF的帖子

模拟整个python类

我正在尝试在python中进行一个简单的测试,但我无法弄清楚如何完成模拟过程.

这是类和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.")
Run Code Online (Sandbox Code Playgroud)

这是我对执行功能的测试:

@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)
Run Code Online (Sandbox Code Playgroud)

由于execute方法尝试建立连接,我需要模拟,我不想做一个真正的连接,只是返回一些mock.我该怎么做?我习惯用Java做测试,但我从来没有在python上做过..

python unit-testing magicmock

13
推荐指数
1
解决办法
8996
查看次数

标签 统计

magicmock ×1

python ×1

unit-testing ×1