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. day01-html

    HTML概述: HTML: Hyper Text Markup Language 超文本标记语言 超文本: 比普通文本功能更加强大,可以添加各种样式 标记语言: 通过一组标签.来对内容进行描述. &l ...

  2. Google Capture The Flag 2018 (Quals) - Beginner's Quest - Reverse - Firmware

    参考链接:https://ctftime.org/task/6263 题目 After unpacking the firmware archive, you now have a binary in ...

  3. 使用Github 当作自己个人博客的图床

    使用Github 当作自己个人博客的图床 前提 本文前提: 我个人博客的草稿是存放在 github上的一个仓库 diarynote 截图存放的图片或者需要放在文章中图片,会固定存放在对应的文件夹中,我 ...

  4. php compact()函数 语法

    php compact()函数 语法 作用:创建包含变量名和它们的值的数组.大理石构件哪家好 语法:compact(var1,var2...) 参数: 参数 描述 var1 必需.可以是带有变量名的字 ...

  5. vue框架搭建--axios使用

    前后端数据交互作为项目最基础需求(静态的除外),同时也是项目中最重要的需求. 本文重点介绍axios如何配合vue搭建项目框架,而axios的详细使用介绍请移步使用说明 1.安装 cnpm insta ...

  6. AppBar中自定义顶部导航

    在上一篇里总结AppBar的一些简单用法,但是AppBar除了有前面那些样式属性外,还能实现类似底部的Tab切换. 首先下载并运行前面的项目: 然后在此基础上实现Tab切换. 常见属性 TabBar有 ...

  7. 20180820-Java 抽象类

    Java 抽象类 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不 ...

  8. [hadoop](2) MapReducer:Distributed Cache

    前言 本章主要内容是讲述hadoop的分布式缓存的使用,通过分布式缓存可以将一些需要共享的数据在各个集群中共享. 准备工作 数据集:ufo-60000条记录,这个数据集有一系列包含下列字段的UFO目击 ...

  9. HTTP 协议解析

    目录 目录 HTTP 协议 HTTP 协议工作原理 HTTP Request 请求行 Request Header HTTP Response 状态行 Response Header Body HTT ...

  10. SSH 的原理和实践

    最近自己在学习使用SSH,现将自己理解的SSH原理和实践SSH的操作写成一篇博客,以供日后查看. 一.SSH是什么?为什么会出现SSH? SSH英文全称是Secure Shell,即安全外壳.首先SS ...