GCP 存储 blob 上的单元测试
Unittest on GCP Storage blob
我有一个关于存储单元测试触发 GCP 云功能的问题。
云函数的结构如下:
from google.cloud import storage
def entry_func(event, context):
# some operations
blob_lists = <bucket_storage>.list_blobs(prefix = f'some_string')
some_other_func(blob_lists)
def some_other_func(list_of_blobs: Iterator):
list1, list2 = [], []
for blob in list_of_blobs:
if blob.name.endswith('some_string'):
list1.append(blob)
elif blob.name.endswith('some_other_string'):
list2.append(blob)
return list1, list2
如何使用 pytest 为这两个函数编写单元测试用例?
我如何模拟 list_of_blobs
对象,因为它是对象的迭代器并检查语句是否有效?
请帮忙
我已经使用下面的测试代码解决了它
import main.py # My source file
class Blob:
def __init__(self, name):
self.name = name
@pytest.fixture
def my_sample_fixture():
return [Blob("sample_some_string"), Blob("sample_some_other_string")]
def test_some_other_func(my_sample_fixture):
list1, list2 = main.some_other_func(my_sample_blobs)
assert "sample_file1.txt" in [file.name for file in list1]
assert "sample_file2.txt" in [file.name for file in list2]
我有一个关于存储单元测试触发 GCP 云功能的问题。
云函数的结构如下:
from google.cloud import storage
def entry_func(event, context):
# some operations
blob_lists = <bucket_storage>.list_blobs(prefix = f'some_string')
some_other_func(blob_lists)
def some_other_func(list_of_blobs: Iterator):
list1, list2 = [], []
for blob in list_of_blobs:
if blob.name.endswith('some_string'):
list1.append(blob)
elif blob.name.endswith('some_other_string'):
list2.append(blob)
return list1, list2
如何使用 pytest 为这两个函数编写单元测试用例?
我如何模拟 list_of_blobs
对象,因为它是对象的迭代器并检查语句是否有效?
请帮忙
我已经使用下面的测试代码解决了它
import main.py # My source file
class Blob:
def __init__(self, name):
self.name = name
@pytest.fixture
def my_sample_fixture():
return [Blob("sample_some_string"), Blob("sample_some_other_string")]
def test_some_other_func(my_sample_fixture):
list1, list2 = main.some_other_func(my_sample_blobs)
assert "sample_file1.txt" in [file.name for file in list1]
assert "sample_file2.txt" in [file.name for file in list2]