如何在python中修补google.cloud.storage

问题描述 投票:0回答:1

我有一个导入Google Cloud的类:

StorageUtils:

from google.cloud import storage

我有一个使用StorageUtils的应用程序:

应用程序:

import StorageUtils

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

测试:

from app import App

我想在不使用Google Cloud的情况下测试我的应用。我发现最简单的方法是使用sys.modules:

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

我发现此解决方案可以解决。还有其他使用python模拟的方法吗?

python-3.x python-mock
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.