HPQC and XLT – Integration Example

You have to work with HP Quality Center (HPQC), but you don’t want to execute all the test cases manually. You automated some tests using XLT Script Developer and like the outcome. You want to use the Script developer much more but you face one last problem: You still have to enter the test results manually into HPQC. This renders some of the test automation advantages useless.

The following example can mitigate that problem. HPQC offers an API called Quality Center Open Test Architecture API (OTA API).
Using this interface, you can set test results automatically.

This is the way to do it: Run the test cases as a Script Developer Batch Test, export the results, parse them and submit them to HPQC. Completely without touching the HPQC web interface at all.

The following Python command line script does the parsing and submitting:

[python]
# -*- coding: utf-8 -*-
import win32com
from win32com.client import Dispatch
from datetime import datetime
from bs4 import BeautifulSoup
import re

# global vars
result_file_name = ‘X:\\path\\to\\xlt-result.html’

# the regular expression for the id that can be found in test case names,
# the id has to be enclosed in brackets so that the regex can contain things
# that are not part of the id
# examples: XLT id: r'(XLT[0-9]{4})[^0-9]’, whole test case name: r'(^.*$)’, r'(T[0-9]{2})[^0-9]’
tc_id_regex = re.compile(r'(T[0-9]{2})[^0-9]’)

# value for strftime to generate the name for the run
run_name_pattern = "Run_%Y-%m-%d-%H-%M-%S"

# hpqc credentials
qcServer = “https://hpqc.server”
qcUser = “username”
qcPassword = “password”
qcDomain = “domain”
qcProject = “project_name”
qcTestCasePath = “Path\to\test\case”
qcTestCaseNode = “test case node name”

# test case status names in hpqc
hpqc_status_pass = ‘Passed’
hpqc_status_fail = ‘Failed’
hpqc_status_no_run = ‘No Run’
hpqc_status_not_completed = ‘Not Completed’
hpqc_status_na = ‘N/A’

# status names in the XLT result file
xlt_result_pass = ‘OK’
xlt_result_fail = ‘FAILED’

# parse the result file
# the processed html structure looks like this:
#
#
#

#

Details #

#

/thead>
#

#

#

#

#

#

#

#

#

#

#

#

#

#

Tests.XLT0001_First_Test_Case_Name OK 0.355
Tests.XLT0002_Second_Test_Case_Name>/td>
#

FAILED 2.255

#
#
result = BeautifulSoup(open(result_file_name))
resultArr = {}
for tr in result.select(“table”)[1].select(“tbody tr”):
tc_name = tr.select(“td”)[0].string[6:]
tc_res = tr.select(“td”)[1].string
tc_id_mo = tc_id_regex.search(tc_name)

if tc_id_mo:
tc_id = tc_id_mo.group(1)
if tc_id in resultArr:
print(“ERROR: there has already been a result for tc_id ” + tc_id + ” tc_name ” + tc_name + ” tc_res: ” + tc_res)
else:
if tc_res == xlt_result_pass:
resultArr[tc_id] = hpqc_status_pass
else:
resultArr[tc_id] = hpqc_status_fail
print(tc_id + ” – ” + tc_name + ” tc_res: ” + tc_res)
else:
print(“ERROR: no valid id found in tc_name ” + tc_name + ” tc_res: ” + tc_res)

# login to hpqc
td = win32com.client.Dispatch(“TDApiOle80.TDConnection.1”)
td.InitConnectionEx(qcServer)
td.Login(qcUser,qcPassword)
td.Connect(qcDomain,qcProject)
if td.Connected:
print(“Logged in to ” + qcProject)
else:
print(“ERROR: Connect failed to ” + qcProject)

# navigate through the test set tree
tsTreeMgr = td.TestSetTreeManager
tsFolder = tsTreeMgr.NodeByPath(qcTestCasePath)
tsList = tsFolder.FindTestSets(qcTestCaseNode)

for tsItem in tsList:
tsTestList = tsItem.TSTestFactory.NewList(“”)

# loop through all test cases in this list
for tsTest in tsTestList:
print(“Test case name: ” + tsTest.TestName)
runFactory = tsTest.RunFactory
run_name = datetime.today().strftime(run_name_pattern)
run = runFactory.AddItem(run_name)
run.CopyDesignSteps() # copies the test steps into this run

# extract the test case id from the test name
tc_id_mo = tc_id_regex.search(tsTest.TestName)
if tc_id_mo:
tc_id = tc_id_mo.group(1)
else:
tc_id = ”
print(“ERROR: no valid id found in name ” + tsTest.TestName)

# get the results from resultArr and set the test result accordingly (identical for test case and all test steps)
if tc_id in resultArr:
status = resultArr[tc_id]
else:
status = hpqc_status_no_run

run.Status = status

# set the status for each test step
tsStepList = run.StepFactory.NewList(“”)
for tsStep in tsStepList:
print(” Test step name: ” + tsStep.Name)
tsStep.Status = status
tsStep.Post()

# submit this run to hpqc
run.Post()
print(“Run added: status: ” + status + ” run_name: ” + run_name)

# close the connection to hpqc
if td.Connected:
td.Disconnect
td.Logout
print(“Logged out from ” + qcProject)

td = None
[/python]

The processed part of the Script Developer result file looks like this:

[html]

Details

Tests.XLT0001_First_Test_Case_Name OK& 0.355
Tests.XLT0002_Second_Test_Case_Name FAILED 2.255 Assertion ‘assertElementPresent(provoke error)’ at line 1 has failed.



[/html]

It needs to run in a Windows environment because it is importing win32com. You have to install:

  • Python (version 3 is needed)
  • pywin32 (Python for Windows extensions)
  • Beautiful Soup 4 (a HTML parser for Python) (after extracting the archive, cd into it and run ‘setup.py install’ to install it)

Now adjust the variables in the script and you are ready to go. The following screenshot depicts the parts of HPQC that the script is using.

In the first td there is the name of the test case (always starting with “Tests.”) and the second td contains the status of the test case.

Disclaimer: This is an example only. Use at your own risk.

The script has been inspired by Generating Custom Reports with Quality Center OTA using Python.