如何在pytest中使用Monkeypatch模拟一个函数多次

问题描述

我要测试的功能是这样的

def function(ssh_client):

    stdin1,stdout1,stderr1 = ssh_client.exec_command("first_command")
    stdout1 = stdout1.readlines()
    if stdout1:
        print("first_command_executed")
        
    stdin2,stdout2,stderr2 = ssh_client.exec_command("second_command")
    stdout = stdout.readlines()
    if stdout2:
        print("second_command_executed")

我正在尝试在pytest中使用Monkeypatch为上述功能创建单元测试用例。这样。

def return_exec_command(*args,**kwargs):
        return "",data,""

    monkeypatch.setattr(ssh_client,'exec_command',return_exec_command)

但是问题是,在执行该补丁时,两次都返回相同的输出。那么,如何为上述功能创建单元测试?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...