Python gadget: Operating HP's quality Center with Python

Source: Internet
Author: User
Tags wrapper

The background is this:

Testers in this group upload test result attachments to QC for each case run. Each function module to be tested may contain dozens of hundred case. So the manual upload test results into a heavy manual labor. Surprisingly, our tool development team said that we could not upload the results of the QC test. To my surprise, the testers actually uploaded the results by hand for the past six months.

Below I have written a gadget to solve this problem:

The idea is simple to invoke the ALM Rest API interface provided by HP. Convert user actions to HTTP requests. Then, according to the interface requirements, upload one attachment to the specified QC test instance.

The main libraries are:
Requests: responsible for sending HTTP requests
BEAUTIFULSOUP4: Responsible for parsing the response returned by the QC server.
The whole tool is very simple.

The steps are as follows:
First let the user log in to QC

[2017-05-17 13:57:25,023] Starting new HTTP Connection (1): xxxxx.com[2017-05-17 13:57:25,430] Post http://XXXXX.com:80/qcbin/ authentication-point/authenticate[2017-05-17 13:57:25,430] headers = {'Authorization ' ' Basic XXXXXXX ' }[2017-05-17 13:57:25,430] ok[2017-05-17 13:57:25,430]--------------------

(The thing with the time stamp above is my log.) Each request will be written down to send something. )

Then according to the user given test_set_id to the QC to search for this test set under the test instance what.

[2017-05-17 13:57:25,430]--------------------[2017-05-17 13:57:26,303] Get http://XXXXX.com:80/qcbin/rest /domains/{domain Name}/projects/{project Name}/test-instances?query={contains-test-set.id[xxxxxx]}&fields=id , test-id&page-size=2000[2017-05-17 13:57:26,303] ok[2017-05-17 13:57:26,303]------------ --------


Then each test instance ID to check the QC in the corresponding test ID, which is the ID of the testing case, and then based on this ID, from the QC to detect the name of the test case.
When searching, this specifies the field name of the return value, the following is the search for test-instance under a test set ID, and requires that the return value contain only the test instance ID and the test ID
test-instances?query={contains-test-set.id[xxx]}&fields=id,test-id&page-size=2000

[2017-05-17 13:57:26,303]--------------------[2017-05-17 13:57:26,516] Get http://XXXXX.com:80/qcbin/rest /domains/xxx/projects/xxx/tests?query={id[xxxxxx]}&fields=name&page-size=2000[2017-05-17 13:57:26,516] ok[2017-05-17 13:57:26,516]--------------------


The Name field value of this test is given again, based on the Test-id found above

The name of the test case was found to match the attachment I want to upload, and the two name pairs will upload an attachment to the test-instance of the use case.

The Upload file section calls a requests request such as:

data = {'filename': ("", attachment_path+attachment_name),'override-existing-attachment':("","y")}file= {'file': Open (Attachment_name,'RB')}response= Self.session.post (URL, files=file,data=data)


Finally, some code is attached for reference:

This class is used to make get requests and post requests. The Auto_log decorator will make a log of what the request was made.
Unlike some QC operations in the library like the full screen of the log

ImportLogging as LogImportRequestsclassrestclient:def __init__(self): Self.headers={} self.session=requests. Session () Log.basicconfig ( level=log.info, format='[% (asctime) s]% (message) s')    defAuto_log (func):defWrapper (*args, * *kw): R= Func (*args, * *kw) Log.info ("%s%s", Func.func_name, Args[1])             forKeyinchKw:log.info ("%s =%s", Key, Kw[key]) Log.info ("%s%s", R.status_code,r.reason) log.info ("--------------------")            returnRreturnwrapper @auto_logdefget (self, url):returnself.session.get (URL) @auto_logdefPost (self, URL, Files=none, data=none,headers=None):ifHeaders isnone:headers=self.headersreturnSelf.session.post (URL, headers=headers,files=files,data=data)


The second class is used to operate the QC, and now I do the user login, search entity, add three functions of the attachment. The common tasks in the reference document are all implemented in this class, along with updating entity and creating entity.

classqcsession:def __init__(self, username, password, qc_url): Self.api_client=restclient () auth= Base64.b64encode ("{}:{}". Format (username, password)) self.api_client.session.headers['Authorization'] ="Basic {}". Format (auth) Self.qc_url=qc_url self.login ()defLogin (self): R= Self.api_client.post (Self.qc_url.auth, headers=self.api_client.session.headers)assert(R.status_code is200)    defGet (Self, *args): R= Self.api_client.get (Self.qc_url.path (*args)) assert(R.status_code is200orR.status_code is201)        returnRdefAdd_attachment (self, filename, over_ride,*args): Data= {            'filename': ("", filename),'override-existing-attachment': ("", Over_ride)} Files= {            'file': Open (filename,'RB')} R= Self.api_client.post (Self.qc_url.path (*args), data=data,files=files)assert(R.status_code is200orR.status_code is201)        returnRdefQuery_entitys (self, entity_name, query_expression, Fields=none, page_size=" -"):        """:p Aram entity_name:the entity to query:p Aram query_expression:the query expression:p Aram fi elds:the files to return in results:p Aram page_size:the result page size, by default is $ which is the Max VA Lue:return:resturn the BeautifulSoup parsed result"""Query_url=u"%s?query={%s}&fields=%s&page-size=%s"%(entity_name,query_expression,fields,page_size) R=self.get (query_url) Res= BeautifulSoup (R.content,"lxml")        returnRes


There is also a class for saving and generating various URL addresses for QC:

classQcurl:def __init__(Self, hostname, domain, project, port=' the'): Self.__base= u'http://{}:{}/qcbin/'. Format (Hostname,port) self.__work= self.__base+ U'rest/domains/{}/projects/{}/'. Format (domain,project) Self.auth= self.__base+ U'authentication-point/authenticate'Self.logout= self.__base+ U'authentication-point/logout/'    defPath (self, *args):returnSelf.__work+'/'. join ([STR (ARG) forArginchArgs])


And the fourth class is to use the QC session class to achieve my business functions. The specific code is not posted, there is nothing to let others reuse things.

Take the top three classes and you can use them.
Give an example:
Log in to QC and query the specified testset_id under all test-instances, and return its ID and Test-id
All of the following variables that are not assigned are converted into strings for the real values of their environment.

Qc_url == qcsession (username, password, qc_url) qc_session.query_entitys ('  Test-instances'contains-test-set.id[%s]'"Id,test-id ")




Hopefully this article will help people with the same problems. The above code is tested on HP ALM 11.52.


Reference Document:/http/alm-help.saas.hpe.com/en/12.53/api_refs/rest_tech_preview/alm_rest_api_tp.html

Python gadget: Operating HP's quality Center with Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.