gpt4 book ai didi

python-3.x - Python unittest.mock google storage - 如何实现 exceptions.NotFound 作为副作用

转载 作者:行者123 更新时间:2023-12-04 14:13:03 32 4
gpt4 key购买 nike

我读过一些tutorials关于在 Python 中进行模拟,但我仍在苦苦挣扎 :-/

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

我想模拟 google.storage.Client().bucket(bucket_name) 方法以针对特定的不存在的存储桶返回 exceptions.NotFound .我正在使用 side_effect 来设置异常(exception)情况

你知道我做错了什么吗?

下面是我尝试过的(我使用了 2 个文件:ma​​in2.pyma​​in2_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 而不是 side 异常效果。

以下是可行的:

@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,这使这一点更清楚。

关于python-3.x - Python unittest.mock google storage - 如何实现 exceptions.NotFound 作为副作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62803901/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com