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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# -*- 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: #<html> #<body> #<table></table> #<h2>Details</h2&> #<table> #<thead>/thead> #<tbody> #<tr> #<td>Tests.XLT0001_First_Test_Case_Name</td> #<td class="success">OK</td> #<td class="number">0.355</td> #<td></td> #</tr> #<tr> #<td>Tests.XLT0002_Second_Test_Case_Name>/td> #<td class="error">FAILED</td> #<td class="number">2.255</td> #<td<Assertion 'assertElementPresent(provoke error)' at line 1 has failed. =></td> #</tr> #</tbody> #</table> #</body> #</html> 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 |
The processed part of the Script Developer result file looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<html> <body> <table></table> <h2>Details</h2> <table> <thead></thead> <tbody> <tr> <td>Tests.XLT0001_First_Test_Case_Name</td> <td class="success">OK&</td> <td class="number">0.355</td> <td></td> </tr> <tr> <td>Tests.XLT0002_Second_Test_Case_Name</td> <td class="error">FAILED</td> <td class="number">2.255</td> <td>Assertion 'assertElementPresent(provoke error)' at line 1 has failed.</td> </tr> </tbody> </table> </body> </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.