作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有构造函数的非常简单的 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
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")
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/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!