如何修改用于测试目的的方法的行为?
# file: cisco_driver.py
class myClass:
def __init__(self, run_commands):
self.run_commands = run_commands
def myFunction(self, interface_name):
command = f"show interface status {interface_name}"
output = self.run_commands(command)
return output
- 函数
myFunction
以接口名称为输入,例如:myFunction(“ gig1/1/1/1”)
- 然后生成命令,在这种情况下:
>显示接口状态gig1/1/1
- 然后使用
run_commands
将SSH用于设备中,执行命令并
以pytest测试的目的检索其输出,我想测试<<强>正在生成的命令,而不是执行此命令。我想绕过run_commands
,然后返回命令本身,而不是其输出。
我该怎么做?是否可以?我可以嘲笑这个吗? monkeypatch?
我希望它的行为:
def myFunction(interface_name):
command = f"show interface status {interface_name}"
return command
# file: cisco_driver.py
class myClass:
def __init__(self, run_commands):
self.run_commands = run_commands
def myFunction(self, interface_name):
command = f"show interface status {interface_name}"
output = self.run_commands(command)
return output
- The function
myFunction
takes an interface name as input, for example:myFunction("Gig1/1/1")
- It then generated a command, in this case:
show interface status Gig1/1/1
- It then uses
run_commands
to SSH into a device, execute the command, and retrieve its output
For the purpose of pytest testing, I want to test the command that is being generated, and not the execution of this command. I want to bypass run_commands
and return the command itself, not the output of it.
How can I accomplish this? Is it possible? Can I mock this? Monkeypatch?
I want it to behave like this:
def myFunction(interface_name):
command = f"show interface status {interface_name}"
return command
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我玩过它,能够找到一个解决方案:
I played around with it and was able to find a solution: