gpt4 book ai didi

python - 无法覆盖由monkeypatch 为单例类设置的函数的初始值

转载 作者:行者123 更新时间:2023-12-04 10:58:29 25 4
gpt4 key购买 nike

我有一个带有构造函数的非常简单的 python 类:

from utils.util import Singleton
class VaultAuth(object):
__metaclass__ = Singleton
def __init__(self, prefix_path, address):

self.path = prefix_path
self.vault_url = address
self.is_authenticated = False

def get_secrets(self, region):
print self.is_authenticated
if not self.is_authenticated:
raise RuntimeError("Failed to fetch secrets")
else:
return True

其中 Singleton 类如下所示:
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]

要编写单元测试,我有:
@pytest.mark.unit_test
def test_get_secrets(monkeypatch):
def mock_init_auth_false(self, *args, **kwargs):
self.path = "dummy_path"
self.vault_url = "dummy_url"
self.is_authenticated = False

def mock_init_auth_true(self, *args, **kwargs):
self.path = "dummy_path"
self.vault_url = "dummy_url"
self.is_authenticated = True

# Negative case - auth is false
monkeypatch.setattr(vault1.VaultAuth, "__init__", mock_init_auth_false)
secrets_manager = vault1.VaultAuth(prefix_path="prefix", address="https://vault")
with pytest.raises(RuntimeError) as exception:
secret_data = secrets_manager.get_secrets(region="test_region")
assert "Failed to fetch secrets" in str(exception.value)
monkeypatch.undo()

# Positive case - auth is true
monkeypatch.setattr(vault1.VaultAuth, "__init__", mock_init_auth_true)
secrets_manager = vault1.VaultAuth(prefix_path="prefix", address="https://vault")
assert secrets_manager.get_secrets(region="test_region")

第一个测试按照预期将 is_authenticated 值打印为 False,但第二个测试也将其打印为 False。如果我颠倒测试的顺序,都会打印 True .任何指针?该类是单例的。如何测试单例类函数?

最佳答案

您是否尝试过在第二个测试用例之前简单地删除单例实例?

@pytest.mark.unit_test
def test_get_secrets(monkeypatch):
def mock_init_auth_false(self, *args, **kwargs):
self.path = "dummy_path"
self.vault_url = "dummy_url"
self.is_authenticated = False

def mock_init_auth_true(self, *args, **kwargs):
self.path = "dummy_path"
self.vault_url = "dummy_url"
self.is_authenticated = True

# Negative case - auth is false
monkeypatch.setattr(vault1.VaultAuth, "__init__", mock_init_auth_false)
secrets_manager = vault1.VaultAuth(prefix_path="prefix", address="https://vault")
with pytest.raises(RuntimeError) as exception:
secret_data = secrets_manager.get_secrets(region="test_region")
assert "Failed to fetch secrets" in str(exception.value)
monkeypatch.undo()

del Singleton._instances[vault1.VaultAuth]

# Positive case - auth is true
monkeypatch.setattr(vault1.VaultAuth, "__init__", mock_init_auth_true)
secrets_manager = vault1.VaultAuth(prefix_path="prefix", address="https://vault")
assert secrets_manager.get_secrets(region="test_region")

关于python - 无法覆盖由monkeypatch 为单例类设置的函数的初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59013272/

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