【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传

时间:2023-03-08 21:43:31

背景

前一篇博客记录的可以上传用例到testlink指定用例集的脚本,内部分享给了之后,同事希望能将testlink上原有的用例下载下来,用于下次修改上传,所有有了本文脚本。

具体实现

获取用例信息

def download_testcase():
"""
获取(下载)testlink上面指定用例集的数据
:return:
"""
datas = []
for data in tlc.getTestCasesForTestSuite(father_id, True, 'full'):
actions = []
expected_results = []
name = data["name"]
summary = data["summary"]
preconditions = data["preconditions"]
importance = data["importance"]
execution_type = data["execution_type"]
author = data["author_id"]
# print(json.dumps(data, indent=4))
for i in range(len(data["steps"])):
actions.append(data["steps"][i]["actions"])
expected_results.append(data["steps"][i]["expected_results"])
datas.append((name, preconditions, '\n'.join(actions), '\n'.join(expected_results), format_execution_type(execution_type), format_auth(author), format_importance(importance), summary))

进行数据转换

def format_execution_type(source_data):
"""
转换执行方式
:param source_data:
:return:
"""
switcher = {
'2': "自动化",
'1': "手工"
}
return switcher.get(source_data, "Param not defind") def format_importance(source_data):
"""
转换优先级
:param source_data:
:return:
"""
switcher = {
'1': "低",
'2': "中",
'3': "高"
}
return switcher.get(source_data, "Param not defind") def format_auth(source_data):
"""
转换作者:可以通过testlink的user表查询到对应id->name对
:param source_data:
:return:
"""
switcher = {
'100': "tester_name",
}
return switcher.get(source_data, "Param not defind")

保存至Excel

def save_suits(file_path, datas):
"""
保存用例
:param file_path: 保存路径
:param datas:
:return:
"""
book = xlrd.open_workbook(file_path, formatting_info=True) # 读取Excel
new_book = copy.copy(book) # 复制读取的Excel
sheet = new_book.get_sheet(0) # 取第一个sheet页
line_num = 1
for i in range(0, len(datas)):
name, preconditions, actions, expected_results, execution_type, author, importance, summary = datas[i]
sheet.write(line_num, 0, u'%s' % name)
sheet.write(line_num, 1, u'%s' % preconditions)
sheet.write(line_num, 2, u'%s' % actions)
sheet.write(line_num, 3, u'%s' % expected_results)
sheet.write(line_num, 4, u'%s' % execution_type)
sheet.write(line_num, 5, u'%s' % author)
sheet.write(line_num, 6, u'%s' % importance)
sheet.write(line_num, 7, u'%s' % summary)
line_num += 1
report_path = os.path.abspath(os.path.join('download'))
if not os.path.exists(report_path):
os.makedirs(report_path)
suits_name = get_suites(father_id)["name"]
new_book.save(os.path.abspath(os.path.join(report_path, '用例集_{}@{}.xlsx'.format(suits_name, time.strftime('%Y.%m.%d@%H%M%S'))))) def get_suites(suite_id):
"""
获取用例集信息
:return:
"""
try:
suites = tlc.getTestSuiteByID(suite_id)
return suites
except testlink.testlinkerrors.TLResponseError as e:
# traceback.print_exc()
logger.warning(str(e).split('\n')[1])
logger.warning(str(e).split('\n')[0])
return

使用方法

环境依赖

环境依赖 安装方法
Python3
xlrd库 pip install xlrd
testlink库 pip install TestLink-API-Python-client
xlutils pip install xlutils

具体方法

  • 将上述的代码保存到一个文件中,底部添加下列代码进行调用
if __name__ == "__main__":
url = "http://localhost/lib/api/xmlrpc/v1/xmlrpc.php"
key = "6c3fe0796142db21" # 这个key是错误的key
tlc = testlink.TestlinkAPIClient(url, key)
father_id = "274539" # 想要下载的用例集的ID,可通过在testlink界面选取用例集,然后点击右键获取
download_testcase()
  • 目录结构参考,与上一篇文章的脚本放在了一起
D:\Project\UPLOAD_DATA2TESTLINK
│ download_testcase.py
│ logger_better.py
│ upload_excel_data.py

└─testCase
down_load_template.xls
  • 在上一步的文件同一目录下创建一个testCase文件夹,创建一个xls文件,文件格式如下:

【Python】实现将testlink上的用例指定格式保存至Excel,用于修改上传