用python script將QC Test plan 執行結果導入excel

 #coding = utf-8
#from __future__ import division
from xlwt import *
""" ------Color-----

#這些均可以從源碼裏面找到。
aqua 0x31
black 0x08
blue 0x0C
blue_gray 0x36
bright_green 0x0B
brown 0x3C
coral 0x1D
cyan_ega 0x0F
dark_blue 0x12
dark_blue_ega 0x12
dark_green 0x3A
dark_green_ega 0x11
dark_purple 0x1C
dark_red 0x10
dark_red_ega 0x10
dark_teal 0x38
dark_yellow 0x13
gold 0x33
gray_ega 0x17
gray25 0x16
gray40 0x37
gray50 0x17
gray80 0x3F
green 0x11
ice_blue 0x1F
indigo 0x3E
ivory 0x1A
lavender 0x2E
light_blue 0x30
light_green 0x2A
light_orange 0x34
light_turquoise 0x29
light_yellow 0x2B
lime 0x32
magenta_ega 0x0E
ocean_blue 0x1E
olive_ega 0x13
olive_green 0x3B
orange 0x35
pale_blue 0x2C
periwinkle 0x18
pink 0x0E
plum 0x3D
purple_ega 0x14
red 0x0A
rose 0x2D
sea_green 0x39
silver_ega 0x16
sky_blue 0x28
tan 0x2F
teal 0x15
teal_ega 0x15
turquoise 0x0F
violet 0x14
white 0x09
yellow 0x0D
"""
   
def cellStyle(**keys):
    #這個以前我是每一類單元格都用了一個函數來處理,後來發現若是要求的style不少,怎麼搞? 因而就採用字典來預先定義好,最後經過**keys來傳須要修改的參數就OK了。

    style_dict = {
    "font_name":'Arial',
    "font_colour_index":0x8,
    "font_bold":False,
    "borders_left":1,
    "borders_right":1,
    "borders_top":1,
    "borders_bottom":1,
    "borders_left_colour":0x08,
    "borders_right_colour":0x08,
    "borders_top_colour": 0x08,
    "borders_bottom_colour":0x08,
    "alignment_horz":0,
    "alignment_vert":2,
    "pattern_fore_colour":31
    }
    if keys == None:
        pass
    else:
        for key in keys:
            style_dict[key] = keys[key]
    
   ##下面這些實例和屬性除了從網上參考,還有就是xlwt庫中的源碼了.好比formatting.py,style.py等。
    fnt = Font()
    borders = Borders()
    fnt.name = style_dict["font_name"]
    fnt.colour_index = style_dict["font_colour_index"]
    fnt.bold = style_dict["font_bold"]
    borders = Borders()
    borders.left = style_dict["borders_left"]
    borders.right = style_dict["borders_right"]
    borders.top = style_dict["borders_top"]
    borders.bottom = style_dict["borders_bottom"]
    borders.left_colour   = style_dict["borders_left_colour"]
    borders.right_colour  = style_dict["borders_right_colour"]
    borders.top_colour    = style_dict["borders_top_colour"]
    borders.bottom_colour = style_dict["borders_bottom_colour"]
    alignment = Alignment()
    alignment.horz = style_dict["alignment_horz"] #HORZ_CENTER = 0x02
    alignment.vert = style_dict["alignment_vert"] #VERT_CENTER = 0x01
    
    pattern = Pattern()
    pattern.pattern = Pattern.SOLID_PATTERN
    pattern.pattern_fore_colour = style_dict["pattern_fore_colour"]
    # "pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12
    # pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on..." 
    
    style = XFStyle()
    style.font = fnt
    style.borders = borders
    style.alignment =alignment
    style.pattern = pattern
    style.num_format_str = "M/D/YY h:mm"
    
    return style
#***********************EXCEL STYLE***************************************
    
#***********************EXCEL Generate***************************************
def generateXLS(fbDict):
    wb = Workbook()
    ws0 = wb.add_sheet('QC Report', cell_overwrite_ok=True)
    
    #*****************************Generate Title*************************************
    title = ["FB","Feature","Case Name","Status","Responsible Tester","Release","SW Build","Priority","ExecDate","Location","Organization","Comments"]
    for col_title in range(0,len(title)):
        style = cellStyle(pattern_fore_colour=18,font_colour_index=13,font_bold=True,alignment_horz=2,alignment_vert=1)
        ws0.write(0, col_title, str(title[col_title]), style)
        col_title += 1
   #*********************************************************************************
   
   #*********************************************************************************     
    fbDict.keys().sort()
    for fblist in fbDict.keys():
#         print "fblist",fblist # "[FB1311]"
        featuredict = fbDict[fblist] # featuredict = {"Feature":LIST}
#         print "featuredict",featuredict
        
        #*******************************
        featuredict.keys().sort()
        start_row = 1
        end_row = 0
        for  Feature in  featuredict.keys():
#             print featuredict.keys()
            #featuredict.keys() = ["Feature","RP00539_6B_1"]
            #featuredict.keys().sort() is the feature, this is to traversal how many feature
            instance_list = featuredict[Feature] #instance_list = ["instance|owner|status|date|comments|build","instance|owner|status|date|comments|build"]
#             print "Feature:",Feature
#             print instance_list
            #int
            lenOfInstancelist = len(instance_list) # lenOfInstancelist = 20,get the count of instance count
            
            end_row += lenOfInstancelist
            #print "end_row",end_row
            
            #wirte the RP00539_6B_1
            wirteFeature(ws0,start_row,end_row,Feature)
            
            #print "lenOfInstancelist:",lenOfInstancelist
            #******************Instance Info***************************
            for instance in instance_list:
#                 print instance
                #caseName = instance[1]
#                 print "instance_data",instance_data
                #print "start_row",start_row
                wirteInstance(ws0,start_row,instance)
                start_row += 1
            #*********************************************************
        #**********************************
        wirteFB(ws0,end_row,fblist)          
    wb.save('QC Report.xls')
def wirteInstance(ws0,instance_row,instance):
    """Write Instance name, tester, statues and so on."""
#     print "wirteInstance:",instance
    #ws0.write(start_row, 3, "instance[i]", Style2())
    for i in range(0,len(instance)):
        #column = i+2
        if instance[i]!=None:
            #這裏是對case執行結果判斷是不是failed,若是是,就將單元格渲染爲紅色。
            if  str(instance[i])=="Failed":
                ws0.write(instance_row, i+2, str(instance[i]), cellStyle(pattern_fore_colour=10))
            else:
                ws0.write(instance_row, i+2, str(instance[i]), cellStyle())
            
            if int(1+len(str(instance[i])=="))*256 <3333:
                #這裏計算單元格中寫的字符串長度若是小於1英寸就不處理,若是大於,就將單元格拉長.由於我實在找不到如何自適應單元格大小。目前不清楚怎麼改進這裏
                pass
            else:
                ws0.col(i+2).width = int(1+len(str(instance[i]))*256)
        else:
            if str(instance[1]) == "Passed" and i==9:
                ws0.write(instance_row, i+2, " ", cellStyle())
            else:
                ws0.write(instance_row, i+2, " ", cellStyle(pattern_fore_colour=10))
        print "Write One case Info to Excel.."
        
                
def wirteFeature(ws0,start_row,end_row,Feature):
    """Write Feature Name: RP00539_6b_1."""
    style = cellStyle(font_bold=True,alignment_horz=2,alignment_vert=1)
    ws0.write_merge(start_row, end_row, 1, 1,Feature, style)
    ws0.col(1).width = int(1+len(Feature)*256)
#     if len(Feature)/12.0 < 1:
#         pass
#     else:
#         ws0.col(1).width = int(len(Feature)/12.0*3333)
def wirteFB(ws0,end_row,fblist):
    """Write Feature Name: FB1311."""
    style = cellStyle(font_bold=True,alignment_horz=2,alignment_vert=1)
    ws0.write_merge(1, end_row, 0, 0,fblist, style)
    ws0.col(0).width = int(1+len(fblist)*256)
#     if len(fblist)/12.0 < 1:
#         pass
#     else:
#         ws0.col(0).width = len(fblist)/12 *3333

基本上WriteEXCEL.py就OK了。
再將QC的內容保存爲一個字典就好了。我保存的是:fbDict = {"FB1311":{"TestSet":["Case Name","Status","Responsible Tester","Release","SW Build","Priority","ExecDate","Location","Organization","Comments"]}} 
看起來比較複雜,哈哈,實際上我比較喜歡用這種嵌套方式,由於源於QC中,一次性,要檢查的TS比較多,因此作了列表方式。最後返回的結果組合我字典。這樣方便接下來的後續處理。

補充上最後的main.py,執行這個腳本就能生成所須要的excel文件。
關於這裏import的GetQCObject.py實際上就是上一篇關於獲取QC testplan改出來的
#coding=utf-8
'''
Created on 2014年1月6日
@author: chenrxia
'''

from WriteEXCEL import *
from GetQCObject import *
 

TEST_SET_PATH = r"Root\Test Sets\ReleaseQ1304\FB1311\New Feature"
TEST_SET_NAME = ["RP000539_6b_1","RP000539_6a_1","RP000539_6b_2","RP000539_6a_2"]
INSTANCE_INFO=True
RUN_INFO=False
 

qcServer = "https://10.135.55.13/qcbin"
qcUser = "user"
qcPassword = "passwd"
qcDomain = "MBB_BTS"
qcProject = "RP_BTS_IV"

tsDict ={}
qcConn = get_QCConnection(qcServer,qcUser,qcPassword,qcDomain,qcProject)
for tsName in TEST_SET_NAME:
    #print tsName
    TestSetList = getInstance(qcConn,TEST_SET_PATH,tsName)   
    InstanceList = lastInfoOfCase(TestSetList,InstanceIfo=INSTANCE_INFO,runInfo=RUN_INFO)
#--+==========================================================================
    tsDict[tsName] =InstanceList
fbDict = {"FB1311":tsDict}  
generateXLS(fbDict)
    
put_QCConnection(qcConn)
raw_input("Finished!")

最終有3個腳本,GetQCObject.py 獲取QC信息WriteEXCEL.py寫excel文件,和main.py最終的調用執行入口. 初學編程,組織的比較亂,請多多指教.python

相關文章
相關標籤/搜索