Python 试图使用补丁来模拟BlobServiceClient,但实际的类被调用
我有以下课程Python 试图使用补丁来模拟BlobServiceClient,但实际的类被调用,python,pytest,azure-storage-blobs,Python,Pytest,Azure Storage Blobs,我有以下课程 from azure.storage.blob import BlobServiceClient class FooContainer: def __init__(self, project_id, account_url, sas_token): service_client = BlobServiceClient(account_url, credential=sas_token) self.container_client = ser
from azure.storage.blob import BlobServiceClient
class FooContainer:
def __init__(self, project_id, account_url, sas_token):
service_client = BlobServiceClient(account_url, credential=sas_token)
self.container_client = service_client.get_container_client("foo")
self.blob_name = f"{project_id}.foo.json"
def upload(self, text):
print(text)
self.container_client.upload_blob(name=self.blob_name, data=text, overwrite=True)
我想在调用upload
时测试container\u client.upload\u blob
调用是否正确,因此我有以下测试
from unittest import mock
from foo_container import FooContainer
@mock.patch('azure.storage.blob.BlobServiceClient', autospec=True)
def test_init(mockBlobServiceClient):
container = FooContainer("x", "y", "z")
container.upload("some text")
mockBlobServiceClient.container_client.upload_blob.assert_called_with(
name="x.foo.json", data="some text", overwrite=True)
当我使用python3-m pytest test\u foo\u container.py运行测试时,BlobServiceClient似乎根本没有被模拟,因为使用FAILED test\u foo\u container.py::test\u init-ValueError测试失败。无法确定共享密钥凭据的帐户名。
完整的故障日志如下所示
================================================================================================= FAILURES =================================================================================================
________________________________________________________________________________________________ test_init _________________________________________________________________________________________________
mockBlobServiceClient = <MagicMock name='BlobServiceClient' spec='BlobServiceClient' id='4572408592'>
@mock.patch('azure.storage.blob.BlobServiceClient', autospec=True)
def test_init(mockBlobServiceClient):
> container = FooContainer("x", "y", "z")
test_foo_container.py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
foo_container.py:5: in __init__
service_client = BlobServiceClient(account_url, credential=sas_token)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/azure/storage/blob/_blob_service_client.py:126: in __init__
super(BlobServiceClient, self).__init__(parsed_url, service='blob', credential=credential, **kwargs)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/azure/storage/blob/_shared/base_client.py:90: in __init__
self.credential = format_shared_key_credential(account, credential)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
account = ['y'], credential = 'z'
def format_shared_key_credential(account, credential):
if isinstance(credential, six.string_types):
if len(account) < 2:
> raise ValueError("Unable to determine account name for shared key credential.")
E ValueError: Unable to determine account name for shared key credential.
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/azure/storage/blob/_shared/base_client.py:324: ValueError
=====================================================================================================================================================================================================================失败=================================================================================================
________________________________________________________________________________________________测试初始化_________________________________________________________________________________________________
mockBlobServiceClient=
@mock.patch('azure.storage.blob.BlobServiceClient',autospec=True)
def测试初始化(mockBlobServiceClient):
>容器=食物容器(“x”、“y”、“z”)
测试容器。py:6:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
foo_container.py:5:in\uu init__
服务\u客户端=BlobServiceClient(帐户\u url,凭证=sas\u令牌)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/azure/storage/blob/_blob_service_client.py:126:in____init__
super(BlobServiceClient,self)。\uuuuu init\uuuuuuuuu(已解析的url,service='blob',credential=credential,**kwargs)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/azure/storage/blob/\u shared/base\u client.py:90:in\u init__
self.credential=格式\共享\密钥\凭证(帐户、凭证)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
帐户=['y'],凭证='z'
def格式共享密钥凭证(帐户、凭证):
如果isinstance(凭证,六种.字符串类型):
如果len(账户)<2:
>raise VALUERROR(“无法确定共享密钥凭据的帐户名”)
E ValueError:无法确定共享密钥凭据的帐户名。
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/azure/storage/blob/_shared/base_client.py:324:ValueError
我做错了什么?为什么BlobServiceClient
没有模拟,而是使用真实的类?模拟时,我们必须指定将使用模拟的模块,而不仅仅是将模拟的模块
一个独立的例子:
src/hello.py
def do_something_在测试()下:
打开(“我的文件”)
test/test\u hello.py
导入模拟
从src导入你好
@mock.patch('src.hello.open')
def测试\你好(模拟\打开):
你好,你在做什么测试吗
mock\u open.assert\u调用了带有('my\u file')的\u
请注意,我们没有模拟open
,而是模拟src.hello.open
在您的示例中,请尝试在@mock.patch('azure.storage.blob.BlobServiceClient')
之前添加模块FooContainer
,该模块定义在:foo\u container
中
我怀疑答案将是@mock.patch('foo_container.BlobServiceClient')
——这是因为您导入BlobServiceClient
的方式非常好,您让我走上了正确的道路。我以前尝试过@mock.patch('foo_container.azure.storage.blob.BlobServiceClient')
,结果是'ModuleNotFoundError:没有名为'foo_container.azure'的模块'foo_container'不是包。我没有想到要做
@mock.patch('foo_container.BlobServiceClient')。运行测试时,我得到了
AttributeError:Mock对象没有“container\u client”属性。我明白为什么会这样。谢谢你的帮助。