Python 3.x 如何在python中修补google.cloud.storage
我有一个导入谷歌云的类: StorageUtils: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
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()