Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在python中修补google.cloud.storage_Python 3.x_Python Mock - Fatal编程技术网

Python 3.x 如何在python中修补google.cloud.storage

Python 3.x 如何在python中修补google.cloud.storage,python-3.x,python-mock,Python 3.x,Python Mock,我有一个导入谷歌云的类: StorageUtils: from google.cloud import storage 我有一个使用StorageUtils的应用程序: from google.cloud import storage 应用程序: 然后我有一个测试,我想测试我的应用程序 测试: 我想在不使用谷歌云的情况下测试我的应用程序。我发现最简单的方法是使用sys.modules: import sys from unittest.mock import MagicMock sys.mo

我有一个导入谷歌云的类:

StorageUtils:

from google.cloud import storage
我有一个使用StorageUtils的应用程序:

from google.cloud import storage
应用程序:

然后我有一个测试,我想测试我的应用程序

测试:

我想在不使用谷歌云的情况下测试我的应用程序。我发现最简单的方法是使用sys.modules:

import sys
from unittest.mock import MagicMock
sys.modules["google.cloud.storage"] = MagicMock()

我发现这个解决方案很难解决。有没有其他方法使用python mock?

在这种情况下,如果包含类StorageUtils的文件名为storage_utils.py:

#test_storage.py
@patch("storage_utils.storage")
def test_storage(st):
    storage = StorageUtils()
#test_storage.py
@patch("storage_utils.storage")
def test_storage(st):
    storage = StorageUtils()