Autocomplete, code completion, warning "Undefined variable ..." for Custom Python Functions, Variables or symbols provided by toolkit wrappers (Python)

Last edited on

Overview

The Squish IDE has some support for code analysis (showing syntax errors, unused variables, etc.) and code completion.

This articles describes the steps necessary to get code analysis and code completion to work in the Squish IDE for your own Python code to get rid of warnings in the Python script editor (such as "Undefined variable: ...").

Approach #1 - Add custom Python module folders to Python project

When Python's import statement the folders containing shared scripts must be configured in either one of the following places:

Step 1: Import error is being shown:

Step 2: Open PyDev Package Explorer view (Window > Show View > Other... > PyDev > PyDev Package Explorer):

Step 3: Add to test suite's PYTHONPATH:

Step 4: Add external libraries and click Force restore internal info:

Step 5: Import error still showing:

Step 6: Close editor and open file again - import error is not showing anymore:

Approach #2 - Add custom Python module folders to Global Scripts

Step 1: Open and select Global Scripts view (Window > Show View... > Squish Tests > Global Scripts).

Step 2: Add the folders with custom Python modules to the Global Script directories:

Handling "Undefined Variable" errors for Symbols provided by Squish Toolkit Wrappers

The symbols "visible" in the scripting language are very dynamic. Many of them are made visible only when an AUT has been hooked up by Squish. This is because Squish can work with a multitude of GUI toolkits, and for each different GUI toolkit different symbols are being exposed.

For example there is a loadUrl() function when you have a Web application, but when you switch to the application context of a Qt application in the same test script, the loadUrl() function will no longer be visible to the test script, until you switch back to the application context of the Web application.

The script editors in the Squish IDE however are not able to distinguish which type of application will be the "current", therefore they cannot simple mark loadUrl() as a function that is available all the time, because it isn't available all the time. Instead, your script code determines when that function is available, by switching application contexts.

To avoid "Undefined Variable" for symbols provided by Squish toolkit wrappers the following approaches can be used.

Approach #1 - Declaring symbols in an external file

Step #1 - Create external file with desired symbols

For this we will create a shared test suite resource, but using a Global Script or a file at an arbitrary path is possible, too.

if False:
    QFontMetrics = None
    QWidget = None
Shared test suite resource "qt_symbols.py"

Step #2 - Make symbol definitions available to PyDev

Step #3 - Import all symbols from the file

if False: from qt_symbols import *

def main():
    fm = QFontMetrics()
test.py

Approach #2 - Declaring symbols in the same test script

if False: QFontMetrics = None

def main():
    fm = QFontMetrics()
test.py

Disabling PyDev's Code Analysis

The Python code analysis can be configured at Edit > Preferences > PyDev > Editor > Code Analysis.