Live update of Python code during debugging, using builtin reload()

Live update of Python code during debugging, using builtin reload()

Introduction

When debugging a Test Script, one can use Python’s built-in reload() from the Squish Script Console to get recent changes to module functions in the currently running Test Execution.

Debugging Python Test Scripts

While debugging your Test Scripts in the Squish IDE, the Script Console might come in handy, e.g. for getting immediate feedback and syntax confirmation, as described in an earlier article.

Sometimes it is more comfortable to modify the code right at its place, in the editor. It is assumed that you’re making use of 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).

For example, let’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().

A Test Case using that function could look like this:

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. reload() is a builtin Python function, that takes an already loaded module as argument.

Now, without leaving your debugging Session, it is possible to make changes to your script functions in the Squish IDE editor, save them and retry its execution until the function is in the desired shape.

What 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

When execution is halted, you can then have the same “Edit Script, Save, call from Script Console” roundtrips as above.

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

Conclusion

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

 

0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*