python-3.x - python 3 : mock a method of the boto3 S3 client

标签 python-3.x unit-testing mocking boto3 botocore

我想对一些调用 boto3 s3 客户端方法的代码进行单元测试。 我无法使用 moto,因为此特定方法 (put_bucket_lifecycle_configuration) 尚未在 moto 中实现。 我想模拟 S3 客户端并确保使用特定参数调用此方法。

我要测试的代码看起来像这样:

# sut.py
import boto3

class S3Bucket(object):
    def __init__(self, name, lifecycle_config):
        self.name = name
        self.lifecycle_config = lifecycle_config

    def create(self):
        client = boto3.client("s3") 
        client.create_bucket(Bucket=self.name)
        rules = # some code that computes rules from self.lifecycle_config
        # I want to test that `rules` is correct in the following call:
        client.put_bucket_lifecycle_configuration(Bucket=self.name, \
          LifecycleConfiguration={"Rules": rules})

def create_a_bucket(name):
    lifecycle_policy = # a dict with a bunch of key/value pairs
    bucket = S3Bucket(name, lifecycle_policy)
    bucket.create()
    return bucket

在我的测试中,我想调用 create_a_bucket()(尽管直接实例化 S3Bucket 也是一个选项)并确保对 put_bucket_lifecycle_configuration 是使用正确的参数创建的。

我弄乱了 unittest.mockbotocore.stub.Stubber 但没能成功破解。除非另有要求,否则我不会发布我的尝试,因为到目前为止它们还没有成功。

我乐于接受有关重构我正在尝试测试的代码的建议,以使其更易于测试。

最佳答案

让测试与以下内容一起使用,其中 ... 是预期传递给 s3.put_bucket_lifecycle_configuration() 的其余参数。

# test.py
from unittest.mock import patch
import unittest

import sut

class MyTestCase(unittest.TestCase):
    @patch("sut.boto3")
    def test_lifecycle_config(self, cli):
        s3 = cli.client.return_value
        sut.create_a_bucket("foo")
        s3.put_bucket_lifecycle_configuration.assert_called_once_with(Bucket="foo", ...)


if __name__ == '__main__':
    unittest.main()

关于python-3.x - python 3 : mock a method of the boto3 S3 client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50536026/

相关文章:

python - 随机产生重复元素

unit-testing - Qt、单元测试和模拟对象

unit-testing - 模拟 OSGi 上下文中的服务激活异常

c# - 我将如何对这个 .Net 代码进行单元测试(允许重构)

python - 为什么我的素数检查代码没有显示正确的输出?

python - 无法停止吃皮卡

python - 双向搜索

node.js - 监视 Date.now() 导致 jasmine-node 没有响应

java - 验证方法是否被调用 - 单元测试

java - 如何在基于代理的Spring bean中设置模拟对象?