General Python Resources¶
Resources for Python 3.8 (default Python version in Squish):
Resources for Python 2.7
Squish Python Basics¶
To execute a test case Squish loads the main test script file ("test.py") and executes the function called "main()". The "main()" function must be declared in the test script. Example of a simple "test.py" script:
Printing / Outputting Values¶
In Python the "print" command is often used for outputting values, for example to read and verify a value. For example, inside of a Squish test script:
In Squish test scripts the output of the print command appears in the Runner/Server Log view (by default at the bottom) in the Squish IDE.
Because the Runner/Server Log also contains a lot of other information, finding ones own output in it is a bit cumbersome, therefore one should use the test.log() command that is provided by Squish instead:
The test.log() command adds an entry with the provided value to the Test Results view in the Squish IDE.
Single line and multi line comments can be put in Python scripts.
Single line comments start with a hash symbol (#):
Comments which span multiple lines can be composed of multiple single line comments or by using multi line comments, which are defined by enclosing the comment in triple double quotes:
Variables act as placeholders for values. The variables can then be used in multiple places where the value that they contain (or represent) should be used:
String Data Type¶
A string is a sequences of characters.
String values are defined in test scripts by enclosing the desired text within single or double quotes (so called string literals):
The backslash character has a special meaning in string literals. To have a backslash in a string literal the backslash must be doubled to escape its special meaning:
Integer Data Type¶
Integer are number values.
Integer values are defined in test scripts by entering the number value directly.
For outputting via test.log() conversion to string is necessary. For this the str() function should be used:
The If statement can be used to execute a piece of code when a certain condition is given. For example:
Additional conditions can be checked separately via elif and then only the respective set of commands that belong to that condition will be executed:
If none of the specified conditions are true, the else part of the If statement can be used to execute code for that case, too:
The conditions specified for the If statement as well as other statements can consist of multiple conditions, too.
Multiple conditions can be evaluated together by using "and", "or" and "not":
Indentation matters in Python¶
In Python scripts indentation is relevant. Random indentation is not allowed.
Less indentation denotes that the code following no longer belongs to the previous block, for example of an if statement:
The recommended indentation is 4 characters (default in Squish IDE) per block. (The above scripts use only 2 characters per block to avoid line wrapping.)
Lists can be used to store multiple values. For example:
Declaring a list with values directly:
- Lists (Tutorial)
*It is possible to iterate over the values of a list or the characters of a string value directly:
Example with range() to generate a list of values from 0 to x:
A more complex example with enumerate if you want the index and the element at the same time:
The while loop repeats until the specified condition is no longer true:
The break command can be used to exit a loop:
We have already seen the mandatory main() function in Squish test scripts. Its purpose is to define a standard starting point for the regular test case execution, so that Squish and the test writer know how and agree on how a test case is being started.
Python generally allows you to create you own functions.
The main purpose of creating functions yourself is to avoid repeating smaller or larger amounts of the same code.
The following example appends values to a list in multiple steps and outputs all list values after each step.
As you can see some code gets repeated a few times, so we have some code redundancy in this example:
The problem with code redundancy is that if the redundant code pieces should all be changed (for example to output the values along with their index in the list), then all code pieces have to be found and changed.
This tends to be error prone because it is easy to miss some of the code pieces.
Another problem with redundancy is that it can make code harder to read and understand.
Redundant code pieces should be put into functions. In the example above the code for outputting all list values is repeated three times, so it is useful to put that into a function and to then call the function instead:
As you can see there is a new function called output_all_list_values() (def output_all_list_values(the_list)) and it requires a list as its parameter when being called (output_all_list_values(my_list)).
The new function has a name that expresses its purposes clearly, so that some of the comments could be removed from the original script, too.
The total number of lines of the above script has only changed slightly (from 21 down to 19), but readability has been improved already, making it easier to see what the script is doing (aside of outputting list values again and again).