如何修改用于测试目的的方法的行为?

发布于 2025-02-11 14:17:50 字数 849 浏览 1 评论 0原文

# 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
  1. 函数myFunction以接口名称为输入,例如:myFunction(“ gig1/1/1/1”)
  2. 然后生成命令,在这种情况下:>显示接口状态gig1/1/1
  3. 然后使用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
  1. The function myFunction takes an interface name as input, for example: myFunction("Gig1/1/1")
  2. It then generated a command, in this case: show interface status Gig1/1/1
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2025-02-18 14:17:50

我玩过它,能够找到一个解决方案:

# pytest_file
def test_case():

    # Initialize new instance of class
    # Use None since "run_commands" will be bypassed
    new_instance = myClass(None)

    # New function that will replace the existing run_commands function
    def mock_run_commands(command)
        # Original behavior of run_commands is to create an SSH connection
        # Instead, I want it to return the same command
        return command

    # Redefine run_commands to my customized function
    new_instance.run_commands = mock_run_commands

    # Pass param through class method
    # This will return "show interface status Gig1/1/1"
    selected_command = new_instance.myFunction("Gig1/1/1")

    # Compare the command produced by class method with what expected outcome
    assert selected_command == expected_command

I played around with it and was able to find a solution:

# pytest_file
def test_case():

    # Initialize new instance of class
    # Use None since "run_commands" will be bypassed
    new_instance = myClass(None)

    # New function that will replace the existing run_commands function
    def mock_run_commands(command)
        # Original behavior of run_commands is to create an SSH connection
        # Instead, I want it to return the same command
        return command

    # Redefine run_commands to my customized function
    new_instance.run_commands = mock_run_commands

    # Pass param through class method
    # This will return "show interface status Gig1/1/1"
    selected_command = new_instance.myFunction("Gig1/1/1")

    # Compare the command produced by class method with what expected outcome
    assert selected_command == expected_command
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文