如何使用unittest将返回值设置为从另一个模拟对象创建的模拟对象?

时间:2019-05-02 15:03:06

标签: python python-unittest

from connection_util import some_engine

class RecommendConversationsUsingOldAPI(BaseResource):

    def check_entitlement(self,msg_ids,usr_id, metadata):
            conn = some_engine.connect()
            some_list = conn.execute(something)

单元测试

@patch('module1.some_engine')  
def test_some_method(self, mockEngine):
        # some code.....

我可以嘲笑some_engine

@patch('module1.some_engine')

如何为return_value设置conn.execute()

1 个答案:

答案 0 :(得分:0)

可以为Mock返回手动创建的connect()对象

 mock_connection = Mock()
 mock_connection.execute.return_value = [1,2,3,4]

 mockEngine.connect.return_value = mock_connection