为了账号安全,请及时绑定邮箱和手机立即绑定

Python Mocking - 如何修补函数内的变量

Python Mocking - 如何修补函数内的变量

蝴蝶刀刀 2021-08-11 20:20:25
我以前从未修补过变量,它甚至可能吗?代码如下:脚本文件import win32guidef code():"""Fetch Window handles by name and set the "test" handle to the foreground"""    open_window = False    top_windows = []    while open_window == False:        # Iterate through Window handles and add them into the "top_windows" list        win32gui.EnumWindows(windowEnumerationHandler, top_windows)        # Find "test" handle and set it to the foreground then break loop        for window in top_windows:            if "test" in window[1]:                win32gui.ShowWindow(window[0],5)                win32gui.SetForegroundWindow(window[0])                open_window = True                break            else:     # Keeping looping until "test" handle has loaded and                pass  # switch to it in the event it hasn't loaded up yet测试脚本.pyimport codefrom unittest.mock import patchdef test_code():  # Question pertains to this line of code. How would I write it? If possible?  with patch(~~~ patch top_windows and give it a value of [("dummy","test")]             so code doesn't loop forever ~~~),\       patch('code.win32gui.SetForegroundWindow') as patch_1,\       patch('code.win32gui.ShowWindow) as patch_2,\       patch('code.win32gui.EnumWindows)as patch_3:           code()   assert patch_1.call_count == 1   assert patch_2.call_count == 2   assert patch_3.call_count == 3如果不修补变量“top_windows”,此代码将无限循环。无论如何,我可以修补它或某种解决方法吗?
查看完整描述

1 回答

?
森栏

TA贡献1810条经验 获得超5个赞

古怪的修复,但我通过将变量转换为函数来使其工作。


查看完整回答
反对 回复 2021-08-11
  • 1 回答
  • 0 关注
  • 210 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信