Python - Specifics of Usage

Applies to TestComplete 15.70, last modified on December 17, 2024

TestComplete supports Python 3.11.5. Below are some specifics of using this Python version in TestComplete. See also Specifics of Usage Common for All Languages.

Functions

  • Duplicate function names are not allowed within a TestComplete script unit.

  • In Python, code execution can start outside a function. In TestComplete, a test run must start from a function. You can start your tests from a function that does not have executable code. This function can contain an ellipsis and (optionally) comments:

    Python

    Log.Message("Test execution will start from the top.")
    
    def Test():
      # The ellipsis in the following line is not executed
      # However, the test will start
      ...

Importing packages

We do not guarantee stable work of your script tests that use third-party Python packages. When you run such tests, you may experience unexpected errors or TestComplete can stop responding.

To import a custom Python package:

  1. Place the package file (.py) to the following folder:

    If you use a 64-bit version of TestComplete:

    <TestComplete>\x64\Bin\Extensions\Python\Python310\Lib

    If you use a 32-bit version of TestComplete:

    <TestComplete>\Bin\Extensions\Python\Python310\Lib

    You can do it by using any file manager, for example, Windows Explorer.

  2. In your script test, use the import command:

    Python

    # Import the package
    import MyModule

If copying the package directly is not possible, set the sys_path variable explicitly and then import the package:

Python

# Set the new path to search for modules
import sys
sys.path.insert(0, "C:\\MyFolder\\MyPackage")

# Import the module
import MyModule

Notes:

References between scripts

  • In Python, you can import units to each other. At that, the Python units of your test project can import units that are not included in the project. TestComplete objects (like Log or Sys) are not available in these units. To use TestComplete objects in these units, include the units in your project, or import them as described in the Importing Tests topic.

  • Python supports circular references between script units.

Multithreading

Though the TestComplete Python library includes the threading module, TestComplete does not support multithreaded Python scripts. Code in the threads that you create will not work. Calls to TestComplete objects, methods and properties from these threads will cause an exception.

print statement

Debugging

  • Set Next Statement command is not supported for Python scripts.

  • In the Inspect dialog, native Python members whose name starts with an underscore ( _ ) are displayed only if the Show hidden members option is enabled.

  • The Inspect dialog is available for Python tuples and lists in the Watch List and Locals panels during debugging.

    Note: If the tuples and lists contain many items, the debugger panels and dialogs group them by 100 for convenient presentation.

Accessing collection items

Python is a strongly typed language. COM objects can have properties that return a collection object. Most scripting languages allow accessing collection items directly, for example, obj.Cells(i, j). TestComplete Python scripts do not support this to avoid ambiguity. To access items of a collection object, you should specify the appropriate property (Item, Items) explicitly:

Python

Log.Message(Excel.Cells.Item[i, j])

Using parameterized properties

To get or set the value of a parameterized property using the default parameter values, use the __getprop__ and __setprop__ methods instead of the property[] syntax:

Python

# Calling a property with parameters
value = obj.property[param1, param2]
obj.property[param1, param2] = "value"

# Calling a property with default parameters
value = obj.property[] # Syntax error!
value = obj.__getprop__("property") # Use this instead

obj.property[] = "value" # Syntax error!
obj.__setprop__("property", "value") # Use this instead

__name__ variable

In TestComplete, the __name__ variable always refers to the name of the unit where the function resides, for example, Unit1. TestComplete never assigns the value "__main__" to this variable. So, the condition

Python

if __name__ == "__main__"

is always false.

Tkinter

Using Tkinter to create GUI elements for your TestComplete tests may cause errors. Use TestComplete User Forms instead of Tkinter.

Assignment expressions

If you have TestComplete version 14.73 or later, you can use the assignment expressions in your tests. The := operator allows assigning values as part of a larger expression. For example:

Python

def Test():
  windows = Sys.FindAllChildren("VisibleOnScreen", "true", 2)
  if (l := len(windows)) > 0:
    Log.Message(aqString.Format("There are %i visible windows on the screen", l))

Functions with a variable number of arguments

In Python scripts, you can declare and use functions with a variable number of arguments. See Python Reference - Arbitrary Argument Lists.

However, there are limitations on calling such functions from keyword tests by using the Run Script Routine operation. To learn more, see the operation description.

Unsupported language features

  • Asynchronous coroutines - TestComplete does not support asynchronous coroutines declared with the async and await statements.

  • Raw byte literals - literals with the rb or br prefixes, such as rb"test". As a workaround, you can create raw bytes objects by converting strings:

    Python

    a = str.encode(r'Test')
    # or
    a = bytes(r'Test', 'utf-8')

See Also

Specifics of Usage Common for All Languages
Script Tests
About Script Tests
Selecting the Scripting Language

Highlight search results