Python - Specifics of Usage

Applies to TestComplete 15.61, last modified on February 27, 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.


  • 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:


    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:


    If you use a 32-bit version of TestComplete:


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

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


    # Import the package
    import MyModule

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


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

# Import the module
import MyModule


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.


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


  • 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:


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:


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

# Calling a property with default parameters
value =[] # Syntax error!
value = obj.__getprop__("property") # Use this instead[] = "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


if __name__ == "__main__"

is always false.


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:


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:


    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