30. January 2020
How to debug Jython Scripts in QF-Test
By the usage of script nodes QF-Test allows embedding of special custom functionality into your test-cases. Typically, these scripts are supposed to be short and relatively simple containing just dedicated functionality, whereas the majority of test logic is done via QF-Test visual nodes.
Verifying the correctness of the functionality of those scripts can be done mostly by inspection or inserting simple print statements for generating debug output into the QF-Test terminal (see also blog about print). Jython also comes with an integrated post mortem debugger to analyze the cause of exceptions within a script execution (see post mortem manual chapter).
Nevertheless there may be situations of more complex scripts that demand for enhanced ways of debugging, e.g. by setting break points, executing step by step and analyzing variable values. To allow such debugging features you can make use of the PyDev, an IDE for Python development, available standalone or as an Eclipse Plugin.
In this article I explain how to prepare an Eclipse or standalone Liclipse IDE to connect to the Jython interpreter of QF-Test and allow enhanced debugging.
If you already use Eclipse IDE the following step 1 shows how to install PyDev as a plugin.
Another option is to directly download the PyDev IDE called Liclipse that already contains the PyDev Plugin (https://www.liclipse.com/). You can skip the first step then.
Step 1: Eclipse Pydev Plugin installation (not required for Liclipse)
Step 2: Configure Jython Interpreter in Eclipse / Liclipse
- Open Menu Window → Preferences
- Choose PyDev → Interpreters → Jython Interpreter
- Browse for Jython jar
- Navigate to lib\jython.jar inside your QF-Test version specific installation directory
- An error message occurs, but please proceed anyways
- Add the QF-Test jython\Lib directory
- New folder
- Select the jython\Lib directory from inside your QF-Test version specific directory to the Python path
By this the basic Eclipse / Liclipse configuration is completed.
Step1: Add PyDev to the QF-Test python path
In order to add the necessary pydev modules to the python path, you can start QF-Test with the additional command line parameter
-J-Dpython.path=<path to relevant pydev modules>
The relevant pydev modules are located in the pysrc directory inside the pydev.core plugin. The pydev.core plugin resides in the eclipse/liclipse plugins folder, starting with org.python.pydev.core.
e.g. for Liclipse is may look like:
“C:\Program Files\Brainwy\LiClipse 5.0.3\plugins\org.python.pydev.core_188.8.131.52809011413\pysrc”
one possible location in Eclipse is:
The QF-Test command line or shortcut for Liclipse, should look similar to this:
"C:\Program Files\QFS\QF-Test\qftest-4.6.0-dev\bin\qftest.exe" -reuse -J-Dpython.path="C:\Program Files\Brainwy\LiClipse 5.0.3\plugins\org.python.pydev.core_184.108.40.206809011413\pysrc"
And for Eclipse respectively:
"C:\Program Files\QFS\QF-Test\qftest-4.6.1-dev\bin\qftest.exe" -reuse -J-Dpython.path= "T:\common\eclipse\eclipse-4.11-64\plugins\org.python.pydev.core_220.127.116.11904261721\pysrc"
Step 2: Configure QF-Test to always save Jython temp scripts
QF-Test writes the Jython script nodes into temporary files (called e.g._script3452345823457.py, located in the QF-Test user directory), but typically only once or when an error occurs. Because those temporary files are now used by the PyDev Debugger, it is very recommended to configure QF-Test to update these files whenever the QF-Test Jython script node is changed. This can be achieved by setting a respective QF-Test option by use of a server script node with the content
which needs to be executed once before starting with debugging.
Step 3: Set Eclipse/Liclipse as Command for external editor (optional)
- When having Eclipse/Liclipse around, it may prove useful to be configured as default external editor for QF-Test Jython scripts. This can be achieved by following steps in QF-Test:
- Open the QF-Test Options via Edit → Options
- Select option: Generel → External Applications
- Set the external editor command to point to the Eclipse/Liclipse executable with the parameter
- Press OK
- Now script nodes are directly opened in Eclipse/Liclipse when the Button “edit in external editor” is pressed.
Step 4: Prepare script for remote debugging
- In order to prepare your Jython script to be debugged, you need to add some line of codes
import pydevd pydevd.stoptrace() pydevd.settrace('127.0.0.1', port=5678, suspend=True, stdoutToServer=True, stderrToServer=True, overwrite_prev_trace=True)
→ here execution will be interrupted and control switches to pydev debugger
Debug Jython scripts
Step 1: Start PyDev Debug Server
- In order to allow PyDev to interrupt and control the Jython script execution, the pydev debug server needs to be started in Eclipse/Liclipse
- Move to Debug perspective
- Menu: Pydev → Start Debug Server
Step 2: Debug Jython Server Scripts
- Start the test execution of a Jython Server Script in QF-Test
- Once the script containing the pydevd.settrace statement gets executed, the run gets interrupted
- Further control is possible in Eclipse/Liclipse
- The debug server comes into action
- Execution stack is shown in the left part
- The script node is opened (it is a temporary generated file _script…..)
- 1) execution pauses right after the pydevd.settrace call
- 2) debugger actions can be used to control further execution.
- Variables can be inspected in the right window part
- NOTE: PyDev debugger warnings may appear in the terminal, looking like
pydev debugger: Unable to find real location for: C:\Users\YourN
but they are not supposed to do any harm, just warnings.
Step 3: Debug Jython SUT Scripts
- Especially SUT Scripts are meant to be small and simple in order to influence the SUT as little as possible. All complex or time-consuming task shall be done within server scripts. If you pause an SUT Script in the Debugger this effectively puts your whole SUT-Appliction on hold, which may change its behavior or even crash.
- If there is really the need to debug SUT scripts as well with remote debugging, the pysrc directory need to be added to python.path of the SUT than as well.
- You will need to adapt the start command of your SUT respectively.
As an example you can adapt the start command of the Swing CarConfig in the CarConfig.cmd file in the demo/carconfig directory in your Qf-Test distribution and add a respective python path parameter:
%JAVA_BIN% -cp "../../qflib/qfdemo.jar;../../qflib/qflib.jar;../../lib/looks.jar;../../lib/jgraphx.jar" -Dpython.path=" T:\common\eclipse\eclipse-4.11-64\plugins\org.python.pydev.core_18.104.22.168904261721\pysrc " "de.qfs.apps.qftest.demo.multi.CarConfigurator" %*
After having started the CarConfig demo, you should be able to Debug Jython SUT scripts for this client.
The usage of Eclipse/Liclipse IDE for editing and debugging Jython code in QF-Test can be a very powerful and helpful enhancement for handling more complex scripts.