场景:
robotframework 执行完用例之后,将执行结果报至testlink。
方案1: 通过TestLink-API-Python-client中的RF关键字 每条用例执行完成之后根据状态进行上报结果
方案2:
python 通过 TestLink-API-Python-client 库操作testlink
通过分析测试报告中的output.xml执行结果,通过testlink接口将结果上报。
实现:
新增关键字:testlink_caseid
入参 caseid
Set Test Documentation TestLink.testcaseID=[${caseid}]
每条用例第一行增加testlink_caseid关键字 入参为此对应testlink对应的caseid
通过解析xml拿出testlink对应的caseid
关键字 代码如下:
# -*- coding:utf-8 -*-
__author__ = 'weikai'
import sys
from bs4 import BeautifulSoup
import json, re
import testlink
import os
from robot.api import logger reload(sys)
sys.setdefaultencoding('utf8') class ReportTestLink(object):
def __init__(self):
self.tls = None def connect_testlink(self, TESTLINK_API_PYTHON_SERVER_URL, TESTLINK_API_PYTHON_DEVKEY):
'''
:param TESTLINK_API_PYTHON_SERVER_URL: testlink的接口地址 如:http://IP:PORT/lib/api/xmlrpc/v1/xmlrpc.php
:param TESTLINK_API_PYTHON_DEVKEY: 2e9685efd7d2589b50987f00dc9d10d7 接口秘钥
:return:
'''
os.environ['TESTLINK_API_PYTHON_SERVER_URL'] = TESTLINK_API_PYTHON_SERVER_URL
os.environ['TESTLINK_API_PYTHON_DEVKEY'] = TESTLINK_API_PYTHON_DEVKEY
self.tls = testlink.TestLinkHelper().connect(testlink.TestlinkAPIClient) def report(self, report_xml_path, testplanid, buildname, user, platformname="0"):
'''
:param report_xml_path: rf测试报告xml文件路径
:param testplanid: 测试计划id
:param buildname: 测试计划的name
:param user: 执行用例人,要和秘钥对应 或者测试计划设置为公共
:param platformname: 平台 默认为0
:return:
''' caseoutlist = self._parse_reportxml(report_xml_path)
for i in caseoutlist:
caseid = i.get("caseid", "")
if caseid != '':
try:
self.tls.reportTCResult(testcaseid=caseid, testplanid=testplanid, buildname=buildname,
status=i.get("status", 'f'),
notes=i.get("notes", ""),
user=user, platformname=platformname)
except Exception as e:
logger.info(u"上报测试结果出现异常:%s" % e)
print(e) def _parse_reportxml(self, report_xml_path):
f = open(report_xml_path, "r")
body = f.read()
soup = BeautifulSoup(body, "lxml")
caselist = soup.findAll("test")
a = []
for i in caselist:
case = {}
execution = ''
case['casename'] = i.attrs['name']
if 'TestLink.testcaseID' in i.text:
doclist = i.findAll('doc')
for s in doclist:
if 'TestLink.testcaseID' in s.text:
execution = re.findall(r"TestLink.testcaseID=\[(.+?)\]", s.text, re.S)[0]
execution = execution.replace('[', '').replace(']', '')
if execution == '':
continue
case['caseid'] = execution # 正则进行匹配
status = i.find('status', attrs={'critical': 'yes'}).attrs['status']
if status == 'PASS':
status = 'p'
if status == "FAIL":
status = 'f'
if status == '':
continue
case['status'] = status
case['endtime'] = i.status.attrs['endtime']
case['notes'] = i.find('status', attrs={'critical': 'yes'}).text
a.append(case)
return a if __name__ == "__main__":
rtl = ReportTestLink()
rtl.connect_testlink('http://ip:port/lib/api/xmlrpc/v1/xmlrpc.php', '2e9685efd7d2589b50987f00dc9d10d7')
rtl.report('D:\loadrunnertmp\\temp\RIDEi3hesf.d\output.xml', '', '','')
转载:https://blog.csdn.net/w229051923/article/details/77336503