前面文章记录了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. request.getparameter和 request.getattribute的差别

    request.getAttribute():是request时设置的变量的值,用request.setAttribute("name","您自己的值");来设 ...

  2. 疯狂html5演讲(两):HTML5简经常使用的元素和属性(一个):html5保留经常使用的元素

    html5取出一小部分的元素和属性:主要删除的各种元素和属性与文档相关的风格.例<font>.width等待,html5建议规范css样式表来控制html文档样式. 1.基本元素 < ...

  3. Java 多并发之原子访问(Atomic Access)

    在编程中,一个原子操作是只会出现一次的.一个原子操作在中间不会停止:要么全部发生要么一点也不发生.我们只有在原子操作完成之后才会看到原子操作的具体影响. 甚至是非常简单的表达式能够构造分解为简单操作的 ...

  4. 33、Python.Unix和Linux系统管理指南.(美)基弗特

  5. char* 转换成 CString

    真是有意思: CString mess ;    mess = m_chLocalIP; 这样OK,但是写在一行就会报错 CString mess  = m_chLocalIP; //有问题

  6. Effective C++(15) 在资源管理类中提供对原始资源的访问

      问题聚焦:     资源管理类是为了对抗资源泄露.     如果一些函数需要访问原始资源,资源管理类应该怎么做呢?        关于资源管理的概念总是显得那么的高大上,其实只是抽象一点. 下面用 ...

  7. UVA 141 The Spot Game 斑点游戏。。

     The Spot Game  The game of Spot is played on an NxN board as shown below for N = 4. During the game ...

  8. TFS二次开发的数据统计以PBI、Bug、Sprint等为例(一)

    TFS二次开发的数据统计以PBI.Bug.Sprint等为例(一) 在TFS二次开发中,我们可能会根据某一些情况对各个项目的PBI.BUG等工作项进行统计.在本文中将大略讲解如果进行这些数据统计. 一 ...

  9. 使用反射机制实现jQuery调用ashx类中的指定方法

    使用反射机制实现jQuery调用ashx类中的指定方法   近期用asp.net做个小网站,但又不喜欢使用asp.net的服务器端控件,经过一番思量后确定前端采用原始的html.后台采用Linq to ...

  10. UITableView的style详解

    在默认的UITableViewCell中,主要有三个系统控件,分别是两个Lable和一个imageView,两个Label,imageView(始终在最左边)的布局位置可以通过下面4个设置: UITa ...