【Python】实现将Excel编写的用例上传到testlink指定用例集
此功能已优化: https://github.com/HeBinz/TestCase2Testlink
背景
百科上说TestLink 是基于web的测试用例管理系统,主要功能是测试用例的创建、管理和执行,并且还提供了一些简单的统计功能。其他的信息可以参照他们的官网http://www.testlink.org/
。
楼主所在的项目,需求、提测、测试等等都是使用的是gitlab的一个个issue加标签管理的,用例的维护在开始的时候也是用的它。后来我们的直接上级职位发生了变更,新leader建议我们使用testlink。
试用了一段时间之后,发现一个非常令人诟病的地方--用例导入只支持xml格式,而且它本身的用例编写也不太方便。
这也是我写这个用例导入小工具的初衷。
思路
开始时博主想的是,把Excel编写的用例(Excel应该是所有测试人员编写用例的首选吧)转换为xml格式导入,后来发现更好的办法-封装Python testlink API。
写得比较仓促,还有很多可以改进的地方,先用起来,后续优化。
具体实现
环境依赖
环境依赖 | 安装方法 |
---|---|
Python3 | 略 |
xlrd库 | pip install xlrd |
testlink库 | pip install TestLink-API-Python-client |
目录结构
目录结构如下,testCase目录用于存放测试用例,upload_excel_data.py用于用例转换上传,logger_better.py用于记录日志,。
D:\PROJECT\UPLOAD_DATA2TESTLIN
│ logger_better.py
│ upload_excel_data.py
│
└─testCase
testCase_Example.xlsx
使用方法
登陆testlink后点击上方个人账号进入个人中心,新页面点击 '生成新的秘钥',使用该key替换掉upload_excel_data.py文件中的key值;
使用get_projects_info函数获取项目所在project_id,替换掉upload_excel_data.py中的project_id
使用鼠标选中想要上传用例的用例集,点击右键获取父节点ID,替换掉upload_excel_data.py中的father_id
upload内容
#! /usr/bin/python
# coding:utf-8
"""
@author:Bingo.he
@file: upload_excel_data.py
@time: 2018/05/03
"""
import collections
import testlink
import xlrd
import os
from logger_better import Log
logger = Log(os.path.basename(__file__))
count_success = 0
count_fail = 0
def get_projects_info():
project_ids = []
projects = tlc.getProjects()
for project in projects:
project_ids.append({project['name']: project['id']})
return project_ids
def get_projects_id():
project_ids = []
projects = tlc.getProjects()
for project in projects:
project_ids.append(project['id'])
return project_ids
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
def readExcel(file_path):
"""
读取用例数据
:return:
"""
case_list = []
try:
book = xlrd.open_workbook(file_path) # 打开excel
except Exception as error:
logger.error('路径不在或者excel不正确 : ' + str(error))
return error
else:
sheet = book.sheet_by_index(0) # 取第一个sheet页
rows = sheet.nrows # 取这个sheet页的所有行数
for i in range(rows):
if i != 0:
case_list.append(sheet.row_values(i)) # 把每一条测试用例添加到case_list中
return case_list
def check_excel_data(func):
"""
参数有效性校验
:param func:
:return:
"""
def _check(*args, **kw):
global count_fail
global count_success
# 校验项目ID及测试集ID的有效性
if not args[0] in get_projects_id():
logger.error('project_id is not auth')
return
if not get_suites(args[1]):
logger.error('father_id is not auth')
return
# 检测测试数据的有效性
for k, v in kw.items():
if v == "" and k not in ['summary', 'importance']:
logger.warning("TestCase '{title}' Parameter '{k}' is null".format(title=kw['title'], k=k))
try:
func(args[0], args[1], kw)
count_success += 1
except Exception as e:
logger.error(e)
count_fail += 1
return _check
def format_info(source_data):
"""
转换Excel中文关键字
:param source_data:
:return:
"""
switcher = {
"低": 1,
"中": 2,
"高": 3,
"自动化": 2,
"手工": 1
}
return switcher.get(source_data, "Param not defind")
@check_excel_data
def create_testcase(test_project_id, suits_id, data):
"""
:param test_project_id:
:param suits_id:
:param data:
:return:
"""
# 设置优先级默认值及摘要默认值
if data['importance'] not in [1, 2, 3]:
data['importance'] = 3
if data["summary"] == "":
data["summary"] = "无"
# 初始化测试步骤及预期结果
for i in range(0, len(data["step"])):
tlc.appendStep(data["step"][i][0], data["step"][i][1], data["automation"])
tlc.createTestCase(data["title"], suits_id, test_project_id, data["authorlogin"], data["summary"],
preconditions=data["preconditions"], importance=data['importance'], executiontype=2)
def excute_creat_testcase(test_project_id, test_father_id, test_file_name):
# 对project_id father_id 做有效性判断
if test_project_id not in get_projects_id():
logger.error('project_id is not auth')
return
if not get_suites(test_father_id):
logger.error('father_id is not auth')
return
# 获取用例
test_cases = readExcel(os.path.join('testCase', test_file_name))
if not isinstance(test_cases, collections.Iterable):
return
# 格式化用例数据
for test_case in test_cases:
testCase_data = {
"title": test_case[0],
"preconditions": test_case[1],
"step": list(zip(test_case[2].split('\n'), test_case[3].split('\n'))), # 以换行符作为测试步骤的分界
"automation": format_info(test_case[4]), # 1 手工, 2 自动
"authorlogin": test_case[5],
"importance": format_info(test_case[6]),
"summary": test_case[7]
}
create_testcase(test_project_id, test_father_id, **testCase_data)
logger.info("本次操作共提交 {} 条数据,成功导入 {} 条,失败 {} 条".format(count_success + count_fail, count_success, count_fail))
if __name__ == "__main__":
url = "http://localhost/testlink/lib/api/xmlrpc/v1/xmlrpc.php" # 替换为testlink对应URL
key = "3aca080de61e3e24b5be209a23fa0652" # 这个key是错误的key,登陆testlink后点击上方个人账号进入个人中心,新页面点击 '生成新的秘钥'获取
file_name = "testCase_Example.xlsx"
project_id = "2354879" # 可以通过 print(get_projects_info())获取
father_id = "2054879" # 鼠标选中想要上传用例的用例集,点击右键获取父节点ID
tlc = testlink.TestlinkAPIClient(url, key)
# print("项目信息: ", get_projects_info())
excute_creat_testcase(project_id, father_id, file_name)
logger内容
#! /usr/bin/python
# coding:utf-8
"""
@author:Bingo.he
@file: logger_better.py
@time: 2018/02/12
"""
import logging
import time
import os
cur_path = os.path.dirname(os.path.realpath(__file__))
log_path = os.path.join(cur_path, 'logs')
if not os.path.exists(log_path): os.mkdir(log_path)
class Log():
def __init__(self, logger, logname='{}.log'.format(time.strftime('%Y-%m-%d'))):
self.logname = os.path.join(log_path, logname)
self.logger = logging.getLogger(logger)
self.logger.setLevel(logging.DEBUG)
self.formatter = logging.Formatter('[%(asctime)s]-[%(name)s]-%(levelname)s: %(message)s')
def __console(self, level, message):
fh = logging.FileHandler(self.logname, 'a', encoding='utf-8')
fh.setLevel(logging.DEBUG)
fh.setFormatter(self.formatter)
self.logger.addHandler(fh)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(self.formatter)
self.logger.addHandler(ch)
if level == 'info':
self.logger.info(message)
elif level == 'debug':
self.logger.debug(message)
elif level == 'warning':
self.logger.warning(message)
elif level == 'error':
self.logger.error(message)
self.logger.removeHandler(ch)
self.logger.removeHandler(fh)
fh.close()
def debug(self, message):
self.__console('debug', message)
def info(self, message):
self.__console('info', message)
def warning(self, message):
self.__console('warning', message)
def error(self, message):
self.__console('error', message)
if __name__ == "__main__":
log = Log(os.path.basename(__file__))
log.info("---测试开始----")
log.info("操作步骤1,2,3")
log.warning("----测试结束----")
其他:
- 用例步骤分隔符:当前使用换行符分隔,可修改excute_creat_testcase函数中testCase_data的step参数
- Excel中作者信息必须与提供的key值对应
测试用例格式
在此感谢该API的作者。
Python testlink API Github项目地址
【Python】实现将Excel编写的用例上传到testlink指定用例集的更多相关文章
- 利用windows系统ftp命令编写的BAT文件上传[转]
利用windows系统ftp命令编写的BAT文件上传[转] 利用windows系统ftp命令编写的BAT文件上传[转] 在开发中往往需要将本地的程序上传到服务器,而且用惯了linux命令的人来说.在w ...
- Python + Selenium + AutoIt 模拟键盘实现另存为、上传、下载操作详解
前言 在web页面中,可以使用selenium的定位方式来识别元素,从而来实现页面中的自动化,但对于页面中弹出的文件选择框,selenium就实现不了了,所以就需引用AutoIt工具来实现. Auto ...
- 用NODEJS处理EXCEL文件导入导出,文件上传
參考文章 http://librajt.github.io/2013/08/04/handle-excel-file-with-nodejs/ 对照了 ExcelJS ,https://github. ...
- python实现批量远程执行命令及批量上传下载文件
#!/usr/bin/env python # -*- coding: utf- -*- # @Time : // : # @Author : xuxuedong # @Site : # @File ...
- Word,Excel,pdf,txt等文件上传并提取内容
近期项目需求:1.要用到各种文件上传,下载. 2.并对文件进行搜索. 3.仅仅要文件里包括有搜索的内容,所有显示出来. 今天正好有时间整理一下,方便以后阅读,及对须要用到的朋友提供微薄之力.首先在实现 ...
- python paramiko模拟ssh登录,实现sftp上传或者下载文件
Python Paramiko模块的安装与使用详解 paramiko是短链接,不是持续链接,只能执行你设定的shell命令,可以加分号执行两次命令. http://www.111cn.net/phpe ...
- 基于hi-nginx的web开发(python篇)——表单处理和文件上传
hi-nginx会自动处理表单,所以,在hi.py框架里,要做的就是直接使用这些数据. 表单数据一般用GET和POST方法提交.hi-nginx会把这些数据解析出来,放在form成员变量里.对pyth ...
- JSP中文件的上传于下载演示样例
一.文件上传的原理 1.文件上传的前提: a.form表单的method必须是post b.form表单的enctype必须是multipart/form-da ...
- python使用ftplib模块实现FTP文件的上传下载
python已经默认安装了ftplib模块,用其中的FTP类可以实现FTP文件的上传下载 FTP文件上传下载 # coding:utf8 from ftplib import FTP def uplo ...
随机推荐
- Git的配置和使用
eclipse中Git的配置 可以参考http://www.cnblogs.com/zhxiaomiao/archive/2013/05/16/3081148.html, http://blog.cs ...
- 编程中,static的用法详解
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用.一.面向过程设计中的sta ...
- 170725、Kafka原理与技术
本文转载自:http://www.linkedkeeper.com/detail/blog.action?bid=1016 Kafka的基本介绍 Kafka最初由Linkedin公司开发,是一个分布式 ...
- Linq初探
1.什么是LINQ LINQ是语言集成查询(Language Integrated Query),这项技术是在.net 3.5就已经引入的技术,极大的方便了数据的查询,他可以支持数据库.XML.ADO ...
- LaTeX:Question & Answer
tikz 宏包中循环 foreach 的使用方法 矩阵环境输入 displaystyle 分式与垂直间距的设置 在 LaTeX 中使用 mathrsfs 宏包遇到 "rsfs7.tfm&qu ...
- 为linux扩展swap分区
1.查看当前swap分区使用情况 [root@localhost ~]# swapon -s Filename Type Size Used Priority /dev/sda2 ...
- 利用GridView实现单选效果
1.实现如图所示的单选效果 由于Android提供的单选按钮radiobutton只能单行或单列显示,且样式并不美观,故可用GridView进行改造,实现单选效果,而要实现这样的效果重点就在GridV ...
- 使用递归打印二叉树的左视图 java
使用递归打印二叉树的左视图 java package com.li.jinRiTouTiao; public class PrintLeftView { static class TreeNode{ ...
- java基础知识 构造方法
在java里面,构造方法也就是构造函数 构造函数=构造方法;构造方法是一种特殊的方法,具有以下特点.(1)构造方法的方法名必须与类名相同.(2)构造方法没有返回类型,也不能定义为void,在方法名前面 ...
- 4.10 Routing -- Asynchronous Routing
本节介绍了路由器的一些更高级的功能和处理复杂异步逻辑的能力. 一.A word on promises 1. 在Ember的Router中Ember使用了大量的Promises概念来处理异步逻辑.简而 ...