Live update of Python code during debugging using importlib.reload()

Introduction

When debugging a test script, one can use importlib.reload() offered by the Python standard library 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 Squish's 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 module, e. g.,

>>> aut_helper.addRandomAddressbookEntry()

Screenshot of Squish IDE before reloading

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

>>> import importlib
>>> importlib.reload(aut_helper)
>>> aut_helper.addRandomAddressbookEntry()

This tells the Python interpreter to load the new function definitions into the current test execution. The importlib.reload() function is part of Python's standard library 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.

Screenshot of Squish IDE after reloading

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


def addRandomAddressbookEntry():
    pass

When execution is paused, you can then have the same "Edit script, save, call from Script Console" round trips as above.

>>> import scratch
>>> scratch.addRandomAddressbookEntry()
# ... Edit scratch.py file, save
>>> importlib.reload(scratch)
>>> scratch.addRandomAddressbookEntry()
# ...

Conclusion

Squish IDE and squishrunner work great with the Python's reload() function. This allows you to modify and debug your test script functions while running a test case.

Comments

    The Qt Company acquired froglogic GmbH in order to bring the functionality of their market-leading automated testing suite of tools to our comprehensive quality assurance offering.