Python - Specifics of Usage

Applies to TestComplete 14.30, last modified on November 21, 2019

TestComplete supports Python 3.6.0. 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

To import custom Python packages, put them in the <TestComplete>\Bin\Extensions\Python\Python36\Lib folder and use the import command. If it is impossible, change the sys_path variable explicitly and then import the module.

Python

# Sets the new path
sys.path.insert(0, "C:\\MyFolder\\MyPackage")

# Imports 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

In Python applications, the print statement writes an expression to standard application output. In TestComplete, there is no standard output, so print does nothing. To post log messages, use the methods of the Log object. See Posting Messages to the Log.

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.

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