首页 > 解决方案 > Python从方法调用返回一个模拟对象

问题描述

我有一个制作 dynamoDB 客户端对象的模块。该模块是外部的,不属于我的代码。我正在尝试对 enclosureClass 进行单元测试


class enclosingClass:
    def enclosing_function():
       ddb_client = get_ddb_client(role)
       ddb_client.query()
       
# Module in my code which returns dynamoDB client object

from x.y.common import get_aws_client

def get_ddb_client(role):
    return get_aws_client('dynamodb', assume_role_arn=role)

在我的代码中,我正在获取 aws 客户端对象并对其调用查询。我想嘲笑这两件事

  1. get_aws_client 的返回值应该是一个模拟对象
  2. 调用查询 dynamoDB 应该发生在模拟对象上。

但是当我运行单元测试时,我得到了实际的 dynamoDB 对象并在 dynamoDB 对象上调用查询返回“预期的字节或字节数组,但得到了”MagicMock

我不明白该怎么做。对 python 非常陌生,不知道如何返回一个模拟的 dynamoDb 客户端对象

@mock.patch("x.y.common.get_aws_client")
@mock.patch("boto3.client")
def test_hello_world_task(get_aws_client_mock, mock_client):
    
    get_aws_client_mock.return_value= mock_client
    mock_client.query.return_value = None

    enclosing_class.enclosing_function() # the method call to enclosing function

标签: python-3.xpython-mock

解决方案


您的问题:dynamoDB 客户端正在enclosing_function.

解决方案:在被测函数之外创建对象(被模拟的对象)并将其作为参数传递[也称为依赖注入]。

class enclosingClass:
    def enclosing_function(get_ddb_client):
       ddb_client = get_ddb_client # could avoid this line by directly taking the parameter as ddb_client
       ddb_client.query()


@mock.patch("x.y.common.get_aws_client")
@mock.patch("boto3.client")
def test_hello_world_task(get_aws_client_mock, mock_client):
    
    get_aws_client_mock.return_value= mock_client
    mock_client.query.return_value = None

    enclosing_class.enclosing_function(get_aws_client_mock) # the method call to enclosing function

希望这个答案。谢谢。


推荐阅读