【问题标题】:Python return a mocked object from method callPython从方法调用返回一个模拟对象
【发布时间】:2021-09-06 20:13:53
【问题描述】:

我有一个制作 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.x python-mock


【解决方案1】:

您的问题: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

希望这个答案。谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多