[英]How to monkeypatch the registry connection with pytest
I am currently writing unit tests for my python script. 我目前正在为我的python脚本编写单元测试。 One of my functions has the following code:
我的一个函数有以下代码:
from _winreg import *
aReg = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
aKey = OpenKey(aReg, LIBRARIES_REG_KEY)
count, value, type = QueryInfoKey(aKey)
ver_list = [EnumKey(aKey, x) for x in range(count) if EnumKey(aKey, x).startswith(version)]
I am using monkeypatch for most of my tests. 我在大多数测试中使用monkeypatch。 I was wondering if there was a way to monkeypatch the ConnectRegistry (or even the OpenKey) function to return the data I want.
我想知道是否有办法对ConnectRegistry(甚至是OpenKey)函数进行monkeypatch以返回我想要的数据。 I would feed it different data and test the behavior.
我会提供不同的数据并测试行为。
Is the import actually in the function being tested? 导入实际上是在被测试的函数中吗? If not, would something like this work?
如果没有,这样的事情会起作用吗?
def my_CR(arg1, arg2):
return something_useful
def my_OK(arg1, arg2):
return something_else_useful
def test_myfunc():
ConnectRegistry = my_CR
OpenKey = my_OK
assert function_calling_CR_and_OK(...) == expected_value
Would you also need to monkeypatch OpenKey and EnumKey? 你还需要monkeypatch OpenKey和EnumKey吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.