我想测试get_list_of_objects()
,定义如下。在这个函数中有self.bucket.objects.filter(),我想模拟它来简单地返回我想要的。我认为这是复杂的,因为自定义类S3使用外部库。所以我不确定我是否应该尝试模拟自定义类或外部库
class S3:
def __init__(self, bucket_name="bucket_name"):
self.s3_resource = boto3.resource("s3")
self.bucket = self.s3_resource.Bucket(bucket_name)
self.s3_client = boto3.client("s3")
def get_list_of_objects(self, key_prefix):
key_list = []
for object_summary in self.bucket.objects.filter(Prefix=key_prefix):
key_list.append(object_summary.key)
return key_list
尝试1:在本例中,我只是测试是否将分配给mock方法返回值的空字典转换为列表
import unittest
from unittest.mock import patch
from filepath.filename import S3
class TestS3Class(unittest.TestCase):
"""TestCase for storage/s3.py"""
def setUp(self):
"""Creates an instance of the live S3 class for testing"""
self.s3_instance = S3()
@patch('S3.self.bucket.object.filter')
def test_get_list_of_objects(self, mock_objects_filter):
mock_objects_filter.return_value = {}
self.assertIsInstance(self.s3_instance.get_list_of_objects(key_prefix='key'), list)
这将导致错误:ModuleNotFoundError: No module named 'S3'
目前没有回答
相关问题 更多 >
编程相关推荐