您的位置:軟件測試 > 開源軟件測試 > 開源測試管理工具 > Testlink
Python解析Testlink導(dǎo)出的xml并寫入excel
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2015/9/11 11:09:41 ] 推薦標簽:Testlink 軟件測試工具

  目前測試團隊要在Testlink寫用例,但為了方便呈現(xiàn)還要同時一份excel用例。如果寫兩份,工作量太大了,Testlink雖然有導(dǎo)出功能,但是只能導(dǎo)出xml形式,因此把xml形式轉(zhuǎn)換為excel的艱巨任務(wù)交給我啦!
  下圖是其中一個用例集導(dǎo)出的示范用例截圖:

  說實話,這個格式比較蛋疼,用python里的Elementtree來解析。關(guān)于數(shù)據(jù)存儲格式,想到了每個testcase用字典dic_temp來存,把需要寫入excel的項提取出來,由于每個case可能包括很多step,因此每個step的鍵代表步驟編號,值的話是一個列表list_temp,包括具體步驟和預(yù)期結(jié)果。
#coding=utf-8
from xml.etree import ElementTree
from win32com.client import Dispatch
import win32com.client
import os
class easy_excel:
def __init__(self,filename=None):
self.xlApp=win32com.client.Dispatch('Excel.Application')
if filename:
self.filename=os.getcwd()+"\"+filename
print self.filename
self.xlBook=self.xlApp.Workbooks.Open(self.filename)
else:
self.xlBook=self.xlApp.Workbooks.Open(filename)
self.filename=''
def save(self,newfilename=None):
if newfilename:
self.filename=newfilename
self.xlBook.SaveAs(newfilename)
else:
self.xlBook.Save()
def close(self):
self.xlBook.Close(SaveChanges=0)
def getCell(self,sheet,row,col):
sht=self.xlBook.Worksheets(sheet)
return sht.Cell(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):
sht=self.xlBook.Worksheets(sheet)
sht.Range(sht.Cells(row1,col1),sht.Cells(row2,col2)).Merge()
def setBorder(self,sheet,row,col):
sht=self.xlBook.Worksheets(sheet)
sht.Cells(row,col).Borders.LineStyle=1
def set_col_width(self,sheet):
sht=self.xlBook.Worksheets(sheet)
sht.Columns("D:F").ColumnWidth=30
xls=easy_excel('test.xlsx')
xls.setCell('Sheet1',2,2,u"編號")
xls.setCell('Sheet1',2,3,u"優(yōu)先級")
xls.setCell('Sheet1',2,4,u"操作步驟")
xls.setCell('Sheet1',2,5,u"預(yù)期結(jié)果")
xls.setCell('Sheet1',2,6,u"實際結(jié)果")
xls.set_col_width('Sheet1')
dic_temp={}
root=ElementTree.parse("./test.xml")
lst_node=root.getiterator("testcase")
num=len(lst_node)
row_flag=3
step_num=0
for case in range(0,num):
steps=lst_node[case].find("steps")
step_num=len(steps)
keywords=lst_node[case].find("keywords")
if keywords is not None:
dic_temp['keyword']=keywords[0].attrib["name"]
else:
dic_temp['keyword']=0
for step in steps:
list_temp=[]
list_temp.append(step[1].text)
list_temp.append(step[2].text)
dic_temp[step[0].text]=list_temp
row_start=row_flag
xls.setCell('Sheet1',row_flag,2,case+1)
xls.setCell('Sheet1',row_flag,3,dic_temp['keyword'])
for j in range(1,step_num+1):
xls.setCell('Sheet1',row_flag,4,dic_temp[str(j)][0])
xls.setCell('Sheet1',row_flag,5,dic_temp[str(j)][1])
row_flag+=1
xls.mergeCells('Sheet1',row_start,3,row_flag-1,3)
xls.mergeCells('Sheet1',row_start,2,row_flag-1,2)
for row in range(2,row_flag):
for col in range(2,7):
xls.setBorder('Sheet1',row,col)
xls.save()
xls.close()
print 'success!'
raw_input("Press any key to continue...")
  寫入excel用了win32com,使用它前提是電腦里要有excel軟件,網(wǎng)上雖然有教程但是不是很全,設(shè)置單元格格式我試了很久,個人對win32接口不熟,后來發(fā)現(xiàn)搜一搜vb操作excel的方法然后依葫蘆畫瓢,實現(xiàn)了設(shè)置單元格格式以及合并單元格。下圖是實際效果:

  <p></p>之類的標志是由于導(dǎo)出xml時沒做處理,這個可以通過字符串處理的,在此不贅述了。源碼都在附件里,需要自己新建空的test.xlsx。

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd