在测试期间,我需要模拟一个对象。我目前正在使用Pytest和monkeypatch进行模拟
示例函数:
def isGccInstalled():
gccInstallationFound = False
command = ['gcc', '-v']
process = subprocess.Popen(command, bufsize=1, universal_newlines=True,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if process.stdout.readline:
gccInstallationFound = True
return gccInstallationFound
我的功能测试:
def mock_subprocess_Popen(*args, **kwargs):
return
def test_getSwBlockType(monkeypatch):
monkeypatch.setattr(subprocess, "Popen", mock_subprocess_Popen)
assert isGccInstalled() == "False"
我需要以某种方式模拟process对象,并将自己的字符串写入process.stdout.readline。 我知道我可以使用monkeypatch.setattr模拟单个变量,但我不知道如何模拟对象,甚至是从其他类继承的对象。有没有办法告诉我的mock返回一个可以访问“process.stdout.readline”的虚拟数据结构
目前没有回答
相关问题 更多 >
编程相关推荐