Python unittest.mock谷歌存储-如何实现异常.未发现副作用



我已经阅读了一些关于Python中mocking的教程,但我仍然在挣扎:-/

例如,我有一个函数包装对谷歌存储的调用以编写blob。

我想模拟google.storage.Client((.bucket_name方法,为特定的不存在的bucket返回异常.NotFound。我正在使用side_effect设置异常

你知道我做错了什么吗?

以下是我尝试过的(我使用了两个文件:main2.pymain2_test.py(:

# main2.py
import logging
from google.cloud import storage
def _write_content(bucket_name, blob_name, content):
storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)
try:
blob.upload_from_string(data=content)
return True
except Exception:
logging.error("Failed to upload blob")
raise

# main2_test.py
import pytest
from unittest.mock import patch
from google.api_core import exceptions
import main2

@patch("main2.storage.Client", autospec=True)
def test_write_content(clientMock):
bucket_name = "not_existent_bucket"
clientMock().bucket(bucket_name).side_effect = exceptions.NotFound
with pytest.raises(exceptions.NotFound):
main2._write_content(bucket_name, "a_blob_name", '{}')

示例调用

pytest main2_test.py::test_write_content

结果

platform linux -- Python 3.7.7, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
rootdir: /home/user/project, inifile: pytest.ini
plugins: requests-mock-1.8.0
collected 1 item                                                                                                                                                                                     
main2_test.py::test_write_content FAILED                                                                                                                                                       [100%]
============================================================================================== FAILURES ==============================================================================================
_________________________________________________________________________________________ test_write_content _________________________________________________________________________________________
clientMock = <MagicMock name='Client' spec='Client' id='139881522497360'>
@patch("main2.storage.Client", autospec=True)
def test_write_content(clientMock):
bucket_name = "my_bucket"
clientMock().bucket(bucket_name).side_effect = exceptions.NotFound

with pytest.raises(exceptions.NotFound):
>           main2._write_content(bucket_name, "a_blob_name", '{}')
E           Failed: DID NOT RAISE <class 'google.api_core.exceptions.NotFound'>
main2_test.py:14: Failed
=====================================
FAILED main2_test.py::test_write_content - Failed: DID NOT RAISE <class 'google.api_core.exceptions.NotFound'>
=====================================

您的测试有两个问题:您没有模拟实际应该引发(upload_from_string(的方法,并且您设置了一个异常class,而不是一个作为副作用的异常。

以下方法可行:

@patch("main2.storage.Client", autospec=True)
def test_write_content(clientMock):
blob_mock = clientMock().bucket.return_value.blob.return_value  # split this up for readability
blob_mock.upload_from_string.side_effect = exceptions.NotFound('testing')  # the exception is created here
with pytest.raises(exceptions.NotFound):
main2._write_content("not_existent", "a_blob_name", '{}')

还要注意的是,为bucket调用设置特定参数没有效果,因为它是在mock上调用的,并且该参数被忽略了——我用return_value替换了它,这使它更加清晰。

最新更新