蛙人 / 全部 / 使用内置reload()在调试期间直播Python代码更新

使用内置reload()在调试期间直播Python代码更新

介绍

调试测试脚本时,可以使用Python’s built-in 重新加载() 从SQUISH脚本控制台获取最近在当前运行的测试执行中更改模块函数的更改。

调试ging Python Test Scripts

在调试中智能IDE中调试测试脚本时,脚本控制台可能会派上用场,例如,用于获得立即反馈和语法确认,如 早些时候的文章.

有时,在编辑器中修改其位置更易于修改代码。假设你’重新利用Python’s import mechanism instead of the source() function for bringing in shared code to your script (if not, we explain how to work around that later in this article).

例如,让’s assume there is a aut_helper.py module in /shared/scripts/. This module provides higher level functions that deal with the AUT, like addRandomAddressbookEntry().

使用该功能的测试用例可以如下所示:

import aut_helper
...
def main():
    startApplication("addressbook")
    ...
    aut_helper.addRandomAddressbookEntry()

If addRandomAddressbookEntry() breaks, e.g. due to intended changes in the AUT, you head into the debugging mode, either by choosing the ‘Debug’ option in the Object Not Found dialog, by setting a breakpoint, or simply by pausing the Test Execution from the Control Bar. In the Squish Script Console, you can call functions defined in the aut_helper.py module, e.g.

>>> aut_helper.addRandomAddressbookEntry()

But it is also possible to make changes to the aut_helper.py module using the Squish IDE and (still being in the same Debug Session) invoke

>>> reload(aut_helper)
>>> aut_helper.addRandomAddressbookEntry()

This tells the Python interpreter to load the new function definitions into the current Test Execution. 重新加载() is a builtin Python function, that takes an already loaded module as argument.

现在,在不留下调试会话的情况下,可以在屏蔽IDE编辑器中对脚本函数进行更改,保存它们并重试其执行,直到该功能处于所需的形状。

什么 about source()?

Even if your Test Suite is organized using the source() function, you can make use of reloading, but you would have to use a dedicated script file just for the purpose of composing snippets, and use import for that file from the Squish Script Console:

import names #remove this line, when not using the Scripted Object Map feature
from squish import *

def addRandomAddressbookEntry():
    pass

暂停执行时,您可以同样“编辑脚本,保存,从脚本控制台调用”往返如上所述。

>>> import scratch
>>> scratch.addRandomAddressbookEntry()
... Edit scratch.py file, Save
>>> reload(scratch)
>>> scratch.addRandomAddressbookEntry()
...

结论

squIDE and squishrunner work great with the Python built-in 重新加载() function. This allows you to modify and debug your test script functions while running a test case.

 

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *

复制链接