前面文章记录了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版)的更多相关文章

  1. testlink用例转换工具2018.12版

    首先说明一点,网上有很多资料,但真正可用的很少:在本人经过百度后,发现其实很多案例会因为各种原因而无法最终实现. Testlink用例转换工具,可以大致分为3种工具: 1)EX-Converter由第 ...

  2. Char Tools,方便的字符编码转换小工具

    工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...

  3. testlink用例的导出到Excel

    一直在网上寻找怎么把testlink的用例导出到Excel中,以及把Excel中已经写好的用例导入到Testlink中的方法.根据现网的经验,然后修改了一下.贴出来,以飨有这方面需求的测试同仁. Te ...

  4. 流媒体协议(RTMP、RTSP、UDP、HTTP、MMS)转换小工具(RTSP转成RTMP案例展示)(转)

    源: 流媒体协议(RTMP.RTSP.UDP.HTTP.MMS)转换小工具(RTSP转成RTMP案例展示)

  5. Java 将Excel转为XML

    可扩展标记语言(XML)文件是一种标准的文本文件,它使用特定的标记来描述文档的结构以及其他特性.通常,我们可以通过格式转换的方式来得到XML格式的文件.本文,将通过Java代码介绍如何实现由Excel ...

  6. Python写的大小写转换小工具

    几行代码的小工具,用于进行如下转换 TRANSACTIONS ON CLOUD COMPUTING => Transactions On Cloud Computing orig = 'TRAN ...

  7. 偷懒小工具 - Excel导出公共类

    说明 最近接了一个任务,就是做一个列表的Excel导出功能.并且有很多页面都会使用这个功能. 导出的Excel大体格式如图 很简单的列表,标题加背景色,然后不同类型,显示方式不一样.对齐方式不一样.不 ...

  8. ViewModel中C# Property自动添加OnPropertyChanged处理的小工具, 以及相应Python知识点

    在做WPFMVVM中经常会遇到一些Model.ViewModel的属性添加添加私有字段和更改通知方法来支持Binding. 比如把: public class Test {      public s ...

  9. web图片转换小工具制作

    HTML <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

随机推荐

  1. ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪婪)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat ...

  2. Lightdm:奔跑吧GUI[已解决]

    Fedora替换gdm为lightdm解决无法登陆问题 前两天安装Codeblocks,这货安装了很多包和依赖,直接导致我重启进步去界面,卡在fedora LOGO处,如下 实在忧伤,已经因为折腾不知 ...

  3. 空间闹钟-v1.6更新!

    (假设图片无法显示可查看我的qq空间:http://user.qzone.qq.com/805853418/blog/1398785778) 生活助手系列--空间闹钟================= ...

  4. JCronTab 定时调用

    习惯使用 unix/linux 的开发者应该对 crontab 都不陌生.Crontab 是一个很方便的用于 unix/linux 系统的任务调度命令.JCronTab 则是一款全然依照 cronta ...

  5. 配置serv-u access数据库遇到的一些问题

    配置好access数据库后,需要建个web页面来供用户修改密码,但修改时,提示:odbc被占用无法打开. serv-u一直在保持打开access数据库.我们需要将数据库设置默认打开方式为共享,不锁定. ...

  6. Smarty从配置文件读取的变量

    从配置文件读取的变量 配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到) 第二种语法在变量作为属性值并被引号括住的 ...

  7. awk学习点滴

    1,常量 ARGC:命令行参数个数 包括awk本身命令,但awk的自带option不算在内. ARGV:命令行参数数组 ARGV[0]是awk本身,其他依次提取就好. $ awk -F ' ' 'BE ...

  8. MVC 在控制器中获取某个视图动态的HTML代码

    ASP.NET MVC 在控制器中获取某个视图动态的HTML代码   如果我们需要动态的用AJAX从服务器端获取HTML代码,拼接字符串是一种不好的方式,所以我们将HTML代码写在cshtml文件中, ...

  9. Hashmat the brave warrior - UVa10055

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10055.html 题目描述 Pr ...

  10. CODEFORCES ROUND #273 DIV2

    题目大意: A简单的说就是,有五个人,他们刚开始有B元,经过一系列过程后,给你他们现在分别有的钱,让你求出B(> <难得的傻逼题啊...但是要注意B是正整数!特判0) B有n个人,要分成m ...