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. popen, pclose - process I/O

    SYNOPSIS #include <stdio.h> FILE *popen(const char *command, const char *type); int pclose(FIL ...

  2. python if-else替代三元表达式

    python中判断一个数是否是偶数的常规代码: def _compare(data): if data % 2 == 0: return True else: return False # 调用偶数判 ...

  3. Spring_搭建过程中遇到的问题

    先看一下问题: 1.在web.xml中配置Spring 加载Spring mvc的时候配置如下: <!--配置SpringMVC的前端控制器--> <servlet> < ...

  4. phpstorm 调试时浏览器显示The requested resource / was not found on this server

    1.进入thinkphp项目的public 目录运行以下命令即可 root@jiang:/var/www/tp5# php -S localhost:8080 router.php PHP 7.2.2 ...

  5. mysql错误: waiting for table metadata lock

    今天突然发现truncate一个表都慢到不行,于是 SHOW PROCESSLIST 发现错误:waiting for table metadata lock解决方法:查看information_sc ...

  6. 解决Debug JDK source 无法查看局部变量的问题方案

    一.问题阐述首先我们要明白JDK source为什么在debug的时候无法观察局部变量,因为在jdk中,sun对rt.jar中的类编译时,去除了调试信息,这样在eclipse中就不能看到局部变量的值. ...

  7. APP稳定性测试-monkey执行

    Monkey命令行可用的全部选项 *示例 : adb shell monkey -p cn.lejiayuan.alpha --pct-touch 30 --pct-motion 15 --pct-t ...

  8. 使用 Visual Studio 调试器附加到运行的进程

    为什么调试附加进程? Visual Studio 调试器可以附加到在 Visual Studio 外运行的进程. 可以使用此附加功能执行以下操作: 调试并非在 Visual Studio 中创建的应用 ...

  9. NoSQL数据库的分布式算法详解

    系统的可扩展性是推动NoSQL运动发展的的主要理由,包含了分布式系统协调,故障转移,资源管理和许多其他特性.这么讲使得NoSQL听起来像是一个大筐,什么都能塞进去.尽管NoSQL运动并没有给分布式数据 ...

  10. H700关闭Direct PD Mapping

    Attached Enclosure doesn't support in controller's Direct mapping modePlease contact your system sup ...