目前測(cè)試團(tuán)隊(duì)要在Testlink寫(xiě)用例,但為了方便呈現(xiàn)還要同時(shí)一份excel用例。如果寫(xiě)兩份,工作量太大了,Testlink雖然有導(dǎo)出功能,但是只能導(dǎo)出xml形式,因此把xml形式轉(zhuǎn)換為excel的艱巨任務(wù)交給我啦!
下圖是其中一個(gè)用例集導(dǎo)出的示范用例截圖:
說(shuō)實(shí)話(huà),這個(gè)格式比較蛋疼,用python里的Elementtree來(lái)解析。關(guān)于數(shù)據(jù)存儲(chǔ)格式,想到了每個(gè)testcase用字典dic_temp來(lái)存,把需要寫(xiě)入excel的項(xiàng)提取出來(lái),由于每個(gè)case可能包括很多step,因此每個(gè)step的鍵代表步驟編號(hào),值的話(huà)是一個(gè)列表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"編號(hào)")
xls.setCell('Sheet1',2,3,u"優(yōu)先級(jí)")
xls.setCell('Sheet1',2,4,u"操作步驟")
xls.setCell('Sheet1',2,5,u"預(yù)期結(jié)果")
xls.setCell('Sheet1',2,6,u"實(shí)際結(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...")
寫(xiě)入excel用了win32com,使用它前提是電腦里要有excel軟件,網(wǎng)上雖然有教程但是不是很全,設(shè)置單元格格式我試了很久,個(gè)人對(duì)win32接口不熟,后來(lái)發(fā)現(xiàn)搜一搜vb操作excel的方法然后依葫蘆畫(huà)瓢,實(shí)現(xiàn)了設(shè)置單元格格式以及合并單元格。下圖是實(shí)際效果:
<p></p>之類(lèi)的標(biāo)志是由于導(dǎo)出xml時(shí)沒(méi)做處理,這個(gè)可以通過(guò)字符串處理的,在此不贅述了。源碼都在附件里,需要自己新建空的test.xlsx。