testlink用例转换小工具(excel转为xml,python版)
前面文章记录了testlink的安装方法(CentOS 7下安装xampp和testlink),由于testlink仅支持xml格式的用例导入,研究了下excel转xml的方法,从网上其他网友那里借用了部分代码,自己又补充修改了下,供大家参考,使用的时候要在PC上安装python 2.7。
所有文件在文章最后面的百度网盘上。
一、代码(有两个py文件):
easy_excel.py:
# coding=utf-8
from xml.etree import ElementTree
from win32com.client import Dispatch
import win32com.client
import os
import sys
reload(sys)
sys.setdefaultencoding("utf-8") class easy_excel:
def __init__(self, filename=None):
self.xlApp = win32com.client.Dispatch('Excel.Application') if filename:
self.filename = os.getcwd() + "\\" + filename
# self.xlApp.Visible=True
self.xlBook = self.xlApp.Workbooks.Open(self.filename)
else:
# self.xlApp.Visible=True
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = '' def save(self, newfilename=None):
if newfilename:
self.filename = os.getcwd() + "\\" + newfilename
# if os.path.exists(self.filename):
# os.remove(self.filename)
self.xlBook.SaveAs(self.filename)
else:
self.xlBook.Save() def close(self):
self.xlBook.Close(SaveChanges=0)
self.xlApp.Quit() def getCell(self, sheet, row, col):
sht = self.xlBook.Worksheets(sheet)
return sht.Cells(row, col).Value def setCell(self, sheet, row, col, value):
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Value = value
# 设置居中
sht.Cells(row, col).HorizontalAlignment = 3
sht.Rows(row).WrapText = True def mergeCells(self, sheet, row1, col1, row2, col2):
start_coloum = int(dic_config["start_coloum"])
# 如果这列不存在就不合并单元格
if col2 != start_coloum - 1:
sht = self.xlBook.Worksheets(sheet)
sht.Range(sht.Cells(row1, col1), sht.Cells(row2, col2)).Merge()
# else:
# print 'Merge cells coloum %s failed!' %col2 def setBorder(self, sheet, row, col):
sht = self.xlBook.Worksheets(sheet)
sht.Cells(row, col).Borders.LineStyle = 1 def set_col_width(self, sheet, start, end, length):
start += 96
end += 96
msg = chr(start) + ":" + chr(end)
# print msg
sht = self.xlBook.Worksheets(sheet)
sht.Columns(msg.upper()).ColumnWidth = length
operate.py:
# coding:utf-8
import os
import sys
reload(sys)
sys.setdefaultencoding("utf-8") from easy_excel import easy_excel
class operate():
def __init__(self, ExcelFileName, SheetName):
self.excelFile = ExcelFileName + '.xls'
self.excelSheet = SheetName
self.temp = easy_excel(self.excelFile)
self.dic_testlink = {}
self.row_flag = 3
self.testsuite = self.temp.getCell(self.excelSheet, 2, 1)
self.dic_testlink[self.testsuite] = {"node_order": "", "details": "", "testcase": []}
self.content = ""
self.content_list = [] def xlsx_to_dic(self, SheetName):
while True:
# print 'loop1'
# list_testcase = dic_testlink[testsuite].["testcase"] testcase = {"name": "", "node_order": "", "externalid": "", "version": "", "summary": "",
"preconditions": "", "execution_type": "", "importance": "", "steps": [], "keywords": "P1"}
testcase["name"] = self.temp.getCell(self.excelSheet, self.row_flag, 1)
testcase["summary"] = self.temp.getCell(self.excelSheet, self.row_flag, 3)
testcase["preconditions"] = self.temp.getCell(self.excelSheet, self.row_flag, 4)
execution_type = self.temp.getCell(self.excelSheet, self.row_flag, 7)
if execution_type == "自动":
testcase["execution_type"] = 2
# print self.temp.getCell('Sheet1',self.row_flag,3)
step_number = 1
testcase["keywords"] = self.temp.getCell(self.excelSheet, self.row_flag, 2)
# print testcase["keywords"]
while True:
# print 'loop2'
step = {"step_number": "", "actions": "", "expectedresults": "", "execution_type": ""}
step["step_number"] = step_number
step["actions"] = self.temp.getCell(self.excelSheet, self.row_flag, 5)
step["expectedresults"] = self.temp.getCell(self.excelSheet, self.row_flag, 6)
testcase["steps"].append(step)
step_number += 1
self.row_flag += 1
if self.temp.getCell(self.excelSheet, self.row_flag, 1) is not None or self.temp.getCell(self.excelSheet, self.row_flag, 5) is None:
break
# print testcase self.dic_testlink[self.testsuite]["testcase"].append(testcase)
# print self.row_flag
if self.temp.getCell(self.excelSheet, self.row_flag, 5) is None and self.temp.getCell(self.excelSheet, self.row_flag + 1, 5) is None:
break
self.temp.close()
# print self.dic_testlink def content_to_xml(self, key, value=None):
if key == 'step_number' or key == 'execution_type' or key == 'node_order' or key == 'externalid' or key == 'version' or key == 'importance':
return "<" + str(key) + "><![CDATA[" + str(value) + "]]></" + str(key) + ">"
elif key == 'actions' or key == 'expectedresults' or key == 'summary' or key == 'preconditions':
return "<" + str(key) + "><![CDATA[<p> " + str(value) + "</p> ]]></" + str(key) + ">"
elif key == 'keywords':
return '<keywords><keyword name="' + str(value) + '"><notes><![CDATA[ aaaa ]]></notes></keyword></keywords>'
elif key == 'name':
return '<testcase name="' + str(value) + '">'
else:
return '##########' def dic_to_xml(self, ExcelFileName, SheetName):
testcase_list = self.dic_testlink[self.testsuite]["testcase"]
for testcase in testcase_list:
for step in testcase["steps"]:
self.content += "<step>"
self.content += self.content_to_xml("step_number", step["step_number"])
self.content += self.content_to_xml("actions", step["actions"])
self.content += self.content_to_xml("expectedresults", step["expectedresults"])
self.content += self.content_to_xml("execution_type", step["execution_type"])
self.content += "</step>"
self.content = "<steps>" + self.content + "</steps>"
self.content = self.content_to_xml("importance", testcase["importance"]) + self.content
self.content = self.content_to_xml("execution_type", testcase["execution_type"]) + self.content
self.content = self.content_to_xml("preconditions", testcase["preconditions"]) + self.content
self.content = self.content_to_xml("summary", testcase["summary"]) + self.content
self.content = self.content_to_xml("version", testcase["version"]) + self.content
self.content = self.content_to_xml("externalid", testcase["externalid"]) + self.content
self.content = self.content_to_xml("node_order", testcase["node_order"]) + self.content
self.content = self.content + self.content_to_xml("keywords", testcase["keywords"])
self.content = self.content_to_xml("name", testcase["name"]) + self.content
self.content = self.content + "</testcase>"
self.content_list.append(self.content)
self.content = ""
self.content = "".join(self.content_list)
self.content = '<testsuite name="' + self.testsuite + '">' + self.content + "</testsuite>"
self.content = '<?xml version="1.0" encoding="UTF-8"?>' + self.content
self.write_to_file(ExcelFileName, SheetName) def write_to_file(self, ExcelFileName, SheetName):
xmlFileName = ExcelFileName + '_' + SheetName + '.xml'
cp = open(xmlFileName, "w")
cp.write(self.content)
cp.close() if __name__ == "__main__": fileName = raw_input('enter excel name:')
sheetName = raw_input('enter sheet name:')
sheetList = sheetName.split(" ")
for sheetName in sheetList:
test = operate(fileName, sheetName)
test.xlsx_to_dic(sheetName)
test.dic_to_xml(fileName, sheetName)
print "Convert success!"
os.system('pause')
二、转换方法:
1、将要转换的测试用例文件放置在与py文件的文件夹中,测试用例样式见下图,
将每个“测试集”放在一个Sheet中,每个Sheet的第二行为该“测试集”的名称,如下图,“运行环境测试”为该测试集的名称,
Sheet的名称,建议与测试集的名称一致,如下图:
双击"operate.py"文件,出现控制台窗口,输入excel文件名称,回车,输入要转换的sheet的名称,多个sheet之间以“空格”隔开,
再回车,出现“Convert success!”转换完成。
转换前后的excel及xml文件:
三、导入testlink:
百度网盘:
http://pan.baidu.com/s/1o8fOaPc
testlink用例转换小工具(excel转为xml,python版)的更多相关文章
- testlink用例转换工具2018.12版
首先说明一点,网上有很多资料,但真正可用的很少:在本人经过百度后,发现其实很多案例会因为各种原因而无法最终实现. Testlink用例转换工具,可以大致分为3种工具: 1)EX-Converter由第 ...
- Char Tools,方便的字符编码转换小工具
工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...
- testlink用例的导出到Excel
一直在网上寻找怎么把testlink的用例导出到Excel中,以及把Excel中已经写好的用例导入到Testlink中的方法.根据现网的经验,然后修改了一下.贴出来,以飨有这方面需求的测试同仁. Te ...
- 流媒体协议(RTMP、RTSP、UDP、HTTP、MMS)转换小工具(RTSP转成RTMP案例展示)(转)
源: 流媒体协议(RTMP.RTSP.UDP.HTTP.MMS)转换小工具(RTSP转成RTMP案例展示)
- Java 将Excel转为XML
可扩展标记语言(XML)文件是一种标准的文本文件,它使用特定的标记来描述文档的结构以及其他特性.通常,我们可以通过格式转换的方式来得到XML格式的文件.本文,将通过Java代码介绍如何实现由Excel ...
- Python写的大小写转换小工具
几行代码的小工具,用于进行如下转换 TRANSACTIONS ON CLOUD COMPUTING => Transactions On Cloud Computing orig = 'TRAN ...
- 偷懒小工具 - Excel导出公共类
说明 最近接了一个任务,就是做一个列表的Excel导出功能.并且有很多页面都会使用这个功能. 导出的Excel大体格式如图 很简单的列表,标题加背景色,然后不同类型,显示方式不一样.对齐方式不一样.不 ...
- ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点
在做WPFMVVM中经常会遇到一些Model.ViewModel的属性添加添加私有字段和更改通知方法来支持Binding. 比如把: public class Test { public s ...
- web图片转换小工具制作
HTML <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
随机推荐
- 设计与实现的简单和经常使用的权限系统(五岁以下儿童):不维护节点的深度level,手工计算level,树形结构
以这种方式.和第三的类似介绍.所不同的是.深度未在数据库中存储节点level,添加和更改时间,护.而是,在程序中,实时去计算的. 至于后面的,依照level升序排序,再迭代全部的节点构造树,与第三篇 ...
- Visual Studio Contact
Visual Studio Contact(); 直播笔记 昨天微软干了几件了不起的事:.NET开发环境将开源.跨平台支持(Mac OS X和Linux).多设备支持(WP.Android和iOS ...
- 安装SQL Server 2005 - 初学者系列 - 学习者系列文章
初学者阶段,建议从数据库为基础入手进行学习. 下面介绍微软的SQL Server 2005数据库的安装. 首先,从下列地址获取SQL Server 2005的安装程序. ed2k://|file|cs ...
- python进程池剖析(二)
之前文章中介绍了python中multiprocessing模块中自带的进程池Pool,并对进程池中的数据结构和各个线程之间的合作关系进行了简单分析,这节来看下客户端如何对向进程池分配任务,并获取结果 ...
- C# 图片存入SQL Server数据库
OpenFileDialog openfiledialog1 = new OpenFileDialog(); if (openfiledialog1.ShowDialog() == DialogRes ...
- DataGridView的使用和批量修改
DataGridView的属性:AllowUserToAddRows:如果为true允许用户添加行,false不允许用户添加行ReadOnly:true表示只读.不能修改单元格中的值,false可以对 ...
- 我的Mac应用
笔记内容 我的Mac软件 用Mac已经2年+,主要用来看电影.听音乐.写日记,其实也是因为偶像uSi在用,选择Mac不仅仅是因为Mac编程特别好用,更是一种生活方式 办公软件 iWork超爱iWork ...
- c#中实现登陆窗口(无需隐藏)
C#登录窗口的实现,特点就是不用隐藏. 在入口处打开登陆: static void Main() { Application.EnableVisualStyles(); Application.Set ...
- HttpClient的使用-爬虫学习(一)
Apache真是伟大,为我们提供了HttpClient.jar,这个HttpClient是客户端的http通信实现库,这个类库的作用是接受和发送http报文,引进这个类库,我们对于http的操作会变得 ...
- [Usaco2008 Jan]Cow Contest奶牛的比赛[神奇的FLOYD]
Description FJ的N(1 <= N <= 100)头奶牛们最近参加了场程序设计竞赛:).在赛场上,奶牛们按1..N依次编号.每头奶牛的编程能力不尽相同,并且没有哪两头奶牛的水平 ...