1.写在前面

testlink上传用例一种方法是excel转换为xml,然后再用xml上传,还有一种是调用api进行上传。
最开始写了个转换工具,是将excel转换为xml,然后在testlink里上传,最后发现当模块变多以后xml太多,一个一个上传太麻烦,所以尝试用调用api的方式来上传用例,并且打包成exe小工具。

2.环境

python3.7.4
testlink1.9.14 ( 1.9.14和19都测试通过 )

3.用到的库

tkinter :python自带,用于编写简易的界面

xlrd:第三方库,需要pip安装,读取excel的库,也可以使用pandas代替

TestLink-API-Python-client:第三方库,需要pip安装,提供python和testlink交互的api

4.完成效果图

4.1 用模板写好用例

4.2 选择好上传项目和根目录导入或者子目录导入(这里用的是testlink中文网的演示地址)

4.3 点击导入后弹出导入进度条

4.4 导入成功,查看testlink里的用例


5.部分代码

5.1 testlink方法二次封装

 @dataclass
class ClientTestLink:
"""
testlink二次封装
"""
user_api_pwd: str
client_url: str = "http://你的testlink地址/testlink/lib/api/xmlrpc/v1/xmlrpc.php" def __post_init__(self):
self.tlc = TestlinkAPIClient(self.client_url, self.user_api_pwd) def get_projects(self):
"""获取testLink内所有项目"""
project_list = []
for project in self.tlc.getProjects():
project_list.append([project.get("id"), project.get("name")])
return project_list def get_project_id_by_name(self, project_name):
"""获取项目id根据项目名称"""
return self.tlc.getProjectIDByName(project_name) def get_test_suites(self, project_id):
"""获取指定项目里(需要项目id)的测试用例集"""
test_suite_list = []
test_suites = self.tlc.getFirstLevelTestSuitesForTestProject(project_id)
for test_suite in test_suites:
test_suite_list.append([test_suite.get("id"), test_suite.get("name")])
return test_suite_list def get_test_suite_id(self, project_id, test_suite_name):
"""查询一级目录"""
all_suites = self.get_test_suites(project_id)
for i in all_suites:
if i[1] == test_suite_name:
return i[0]
else:
pass
return False def get_test_suite_for_test_suite(self, test_suite_id):
"""查询用例集下是否含有某用例集"""
try:
test_suite_id = self.tlc.getTestSuitesForTestSuite(test_suite_id)
return test_suite_id
except Exception:
return False def create_test_suite(self, project_id: int, test_suite_name: str, parent_id: int = None):
"""判断是否拥有测试用例集,如果没有就创建测试用例集"""
suite_data = self.tlc.createTestSuite(project_id, test_suite_name, test_suite_name, parentid=parent_id)
cheak_bool = isinstance(suite_data, list)
if cheak_bool:
return suite_data[0].get("id")
else:
if parent_id is None:
return self.get_test_suite_id(project_id=project_id, test_suite_name=test_suite_name)
else:
for k, v in self.get_test_suite_for_test_suite(parent_id).items():
if isinstance(v, dict):
if v.get("name") == test_suite_name:
return v.get("id")
else:
pass
else:
return self.get_test_suite_for_test_suite(parent_id).get("id") def create_test_case(self, project_id: int, test_suite_id: int, test_case_name, summary, preconditions,
step, result, author_login):
"""创建测试用例"""
self.tlc.initStep(step, result, 1)
return self.tlc.createTestCase(testprojectid=project_id,
testsuiteid=test_suite_id,
testcasename=test_case_name,
summary=summary,
preconditions=preconditions,
authorlogin=author_login
) def update_project_keywords(self, project_id, test_case_id, keyword_value):
"""加关键字"""
test_case = self.tlc.getTestCase(testcaseid=test_case_id)[0]
args = {
'testprojectid': project_id,
'testcaseexternalid': test_case['full_tc_external_id'],
'version': int(test_case['version'])
}
keyword = self.tlc.addTestCaseKeywords({args['testcaseexternalid']: [keyword_value]})
return keyword def update_custom_field(self, project_id, test_case_id, custom_fields: dict):
"""更新自定义字段"""
test_case = self.tlc.getTestCase(testcaseid=test_case_id)[0]
args = {
'testprojectid': project_id,
'testcaseexternalid': test_case['full_tc_external_id'],
'version': int(test_case['version'])
}
custom = self.tlc.updateTestCaseCustomFieldDesignValue(
args['testcaseexternalid'], args['version'], args['testprojectid'], custom_fields)
return custom

5.2 从根目录上传用例

 def run_root(excel_file_name, project_id, username, api_token):
"""
创建用例
"""
case_num = get_all_case_num(excel_file_name)
win2 = tk.Tk()
# 设置标题
win2.title("导入任务")
# 设置大小和位置
win2.geometry("220x100")
# 禁止改变窗口大小
win2.resizable(0, 0)
mpb = ttk.Progressbar(win2, orient="horizontal", length=150, mode="determinate")
mpb.place(x="", y="")
mpb["maximum"] = case_num
mpb["value"] = 0
upload_label = tk.Label(win2, text='正在导入用例...(切勿关闭)', fg='red')
upload_label.place(x="", y="")
upload_label_text = tk.Label(win2, text='', fg='red')
upload_label_text.place(x="", y="")
upload_per_label = tk.Label(win2, text='', fg='red')
upload_per_label.place(x="", y="")
# 读取excel,获取数据
datacases = xlrd.open_workbook(excel_file_name) sheets = datacases.sheet_names() for sheet in sheets:
sheet_1 = datacases.sheet_by_name(sheet) # ====================测试用例功能模块============================== row_num = sheet_1.nrows
for i in range(1, row_num):
# 定义默认步骤编号第一步
catalog_1 = sheet_1.cell_value(i, 0) # 一级目录
catalog_2 = sheet_1.cell_value(i, 1) # 二级目录
catalog_3 = sheet_1.cell_value(i, 2) # 三级目录
test_case_name = sheet_1.cell_value(i, 3) # 用例名称
summary = sheet_1.cell_value(i, 4) # 摘要
key_words = sheet_1.cell_value(i, 5) # 关键字
test_case_level = sheet_1.cell_value(i, 6) # 用例级别
preconditions = sheet_1.cell_value(i, 7) # 预置条件
step = sheet_1.cell_value(i, 8) # 操作步骤
step_list = []
# 处理换行
for i_step in step.split('\n'):
step_list.append("<p>" + i_step + "</p>")
step = ''.join(step_list)
expected_results = sheet_1.cell_value(i, 9) # 预期结果
expected_results_list = []
# 处理换行
for i_expected_results in expected_results.split('\n'):
expected_results_list.append("<p>" + i_expected_results + "</p>")
expected_results = ''.join(expected_results_list) # 创建一级目录
test_suite_id = ClientTestLink(api_token).create_test_suite(project_id=project_id,
test_suite_name=catalog_1)
# 创建二级目录
if catalog_2:
test_suite_id = ClientTestLink(api_token).create_test_suite(project_id=project_id,
test_suite_name=catalog_2,
parent_id=test_suite_id)
# 创建三级目录
if catalog_3:
test_suite_id = ClientTestLink(api_token).create_test_suite(project_id=project_id,
test_suite_name=catalog_3,
parent_id=test_suite_id)
result = ClientTestLink(api_token).create_test_case(
project_id=project_id,
test_suite_id=test_suite_id,
test_case_name=test_case_name,
summary=summary,
preconditions=preconditions,
step=step,
result=expected_results,
author_login=username) test_case_id = result[0].get("id")
# 添加关键字
ClientTestLink(api_token).update_project_keywords(project_id=project_id,
test_case_id=test_case_id,
keyword_value=key_words)
# 添加自定义字段
ClientTestLink(api_token).update_custom_field(project_id=project_id,
test_case_id=test_case_id,
custom_fields={"优先级": test_case_level})
mpb["value"] = i
upload_label_text.config(text=f"<{i}/{case_num}>")
upload_per_label.config(text=f"{((i / case_num) * 100):.2f}%")
win.update()
print(f"{test_case_id}-上传用例成功")
win2.destroy()

6.写在最后

等稍后整理好后会把整个源码放出来,因为写的比较着急,很多代码逻辑没考虑到,欢迎指出,指出必改。

【Python】Python实现Excel用例直接导入testlink-UI界面小工具的更多相关文章

  1. 快速读取csv平面文件,并导入数据库,简单小工具

    using DataToDeal; using LumenWorks.Framework.IO.Csv; using Microsoft.Win32; using System; using Syst ...

  2. 用Python写个自动ssh登录远程服务器的小工具

    很多时候我们喜欢在自己电脑的终端直接ssh连接Linux服务器,而不喜欢使用那些有UI界面的工具区连接我们的服务器.可是在终端使用ssh我们每次都需要输入账号和密码,这也是一个烦恼,所以我们可以简单的 ...

  3. testlink用例转换小工具(excel转为xml,python版)

    前面文章记录了testlink的安装方法(CentOS 7下安装xampp和testlink),由于testlink仅支持xml格式的用例导入,研究了下excel转xml的方法, 从网上其他网友那里借 ...

  4. 使用Python将xmind脑图转成excel用例(一)

    最近接到一个领导需求,将xmind脑图直接转成可以导入的excel用例,并且转换成gui可执行的exe文件,方便他人使用. 因为对Python比较熟悉,所以就想使用Python来实现这个功能,先理一下 ...

  5. 使用Python将Excel中的数据导入到MySQL

    使用Python将Excel中的数据导入到MySQL 工具 Python 2.7 xlrd MySQLdb 安装 Python 对于不同的系统安装方式不同,Windows平台有exe安装包,Ubunt ...

  6. python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图

    python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...

  7. Python实现XMind测试用例快速转Excel用例

    转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/c2d10f21.html 你好,我是测试蔡坨坨. 今天分享一个Python编写的小工具,实现XMind测试用例转Excel用 ...

  8. python读取excel一例-------从工资表逐行提取信息

    在工作中经常要用到python操作excel,比如笔者公司中一个人事MM在发工资单的时候,需要从几百行的excel表中逐条的粘出信息,然后逐个的发送到员工的邮箱中.人事MM对此事不胜其烦,终于在某天请 ...

  9. 使用Python操作Office——EXCEL

    首先介绍下office win32 com接口,这个是MS为自动化提供的操作接口,比如我们打开一个EXCEL文档,就可以在里面编辑VB脚本,实现我们自己的效果.对于这种一本万利的买卖,Python怎么 ...

随机推荐

  1. Docker实战部署应用——Redis

    Redis 部署 拉取Redis镜像 docker pull redis 创建Redis容器 docker run -id --name=sun_redis -p 6379:6379 redis 客户 ...

  2. GitHub 搭建博客,出现 hexo g -d 报错

    想搭建一个个人博客,但是在将博客推送到Github上的时候在git bash 下运行hexo g -d命令出现错误: 错误如下:  fatal: HttpRequestException encoun ...

  3. 2018-8-10-调试-ms-源代码

    title author date CreateTime categories 调试 ms 源代码 lindexi 2018-08-10 19:17:19 +0800 2018-2-13 17:23: ...

  4. 2019-3-9-通过-frp-开启服务器打开本地的-ZeroNet-服务器外网访问

    title author date CreateTime categories 通过 frp 开启服务器打开本地的 ZeroNet 服务器外网访问 lindexi 2019-03-09 11:47:4 ...

  5. oralce 汇编02

    Assembler Directives .align integer, padThe .align directive causes the next data generated to be al ...

  6. YOLOV3算法详解

     YOLOV3 YOLO3主要的改进有:调整了网络结构:利用多尺度特征进行对象检测:对象分类用Logistic取代了softmax. 新的网络结构Darknet -53 darknet-53借用了re ...

  7. VUE 生成二维码插件

    原文:https://www.jianshu.com/p/496fd1cbee8d npm install qrcodejs2 --save 页面中引入 dom 结构 JS 方法编写 export d ...

  8. 给mongodb设置密码

    内容来自:https://segmentfault.com/a/1190000011554055 mongodb安装后是无需密码 Mongodb安装后自身是没有密码的,用户连接只需填写id地址,端口号 ...

  9. 简单的使用redis

    心不慌手不抖我们跟着大哥走 https://blog.csdn.net/zhangcongyi420/article/details/82686702

  10. 前端通过axios和FormData实现文件上传功能遇到的坑

    使用element-ui中的upload上传组件,前端上传数据参数已经传过去了,但是后端 (java) 接不到数据 (null) [解决方案] html部分: <el-button type=& ...