Running QTP tests using Python

Running QTP tests using Python
Photo by Maxim Hopman / Unsplash

QTP provides an interface called the automation object model. This model is essentially a COM interface providing a bunch of objects that can be used to automate QTP. The full object list is available in the QuickTest Professional Automation documentation.

Running QTP tests from the command line is useful for doing scheduled automatic testing. If you use a continuous integration system to do automatic builds of your software, you can run your QTP tests on the latest build.

The following is a Python script that is able to run a test and print out Passed or Failed.  It is a direct port of example code in the documentation written in VBScript

import win32com, win32com.client

qtp = win32com.client.Dispatch("QuickTest.Application")

# starts up QTP

# make the QTP window visible
qtp.Visible = True

# Open a test, replace the path 

# to open a QTP test in Quality Center
# qtp.Open(r"[QualityCenter] Subject\FolderName\QTPScript")

# create a RunResultsOptions object
qtResultsOpt = win32com.client.Dispatch("QuickTest.RunResultsOptions")

# set the location to where the results will be save
qtResultsOpt.ResultsLocation = "C:\Test\test1\res"


print "Test has %s" %qtp.Test.LastRunResults.Status

# close the Test

# quit QTP