HTML報告V1.3 根據文字內容顯示不同的字體顏色:
代碼如下:

1 # -*- coding=utf-8 -*- 2 import time,os 3 4 """ 5 V1.2 6 1.生成total表格 7 2.生成report表格 8 3.生成module表格 9 4.生成case表格 10 11 12 """ 13 #----數據部分------------------------------------------- 14 func_dict={"funcname":"模塊1",} 15 16 funcname=['書架','書城','分類','我的'] 17 case1={"name":"模塊1","total":"10","passnum":"10","failnum":"0","radio":"80","status":"PASS"} 18 case2={"name":"模塊2","total":"20","passnum":"15","failnum":"5","radio":"75","status":"Fail"} 19 20 VERSION_DICT={"version": '快看小說 3.8.8',"radio":'99',"runstarttime":time.strftime('%Y-%m-%d %H:%M:%S'),"runstoptime" : time.strftime('%Y-%m-%d %H:%M:%S')} 21 22 #---END------------------------------------------- 23 24 class Template_mixin(object): 25 """html報告""" 26 # ------------------------------------------------------------------------ 27 # HTML Template 28 HTML_TMPL = r""" 29 <!DOCTYPE html> 30 <html lang="en"> 31 <head> 32 <meta charset="UTF-8"> 33 <title>%(title)s</title> 34 <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> 35 <h2 style="font-family: Microsoft YaHei">%(title)s</h2> 36 37 <p class='attribute'><strong>測試結果 : </strong> %(total)s</p> 38 <style type="text/css" media="screen"> 39 body { font-family: Microsoft YaHei,Tahoma,arial,helvetica,sans-serif;padding: 20px;} 40 </style> 41 </head> 42 <body> 43 <table id='result_table' class="table table-condensed table-bordered table-hover"> 44 <colgroup> 45 <col align='left' /> 46 <col align='right' /> 47 <col align='right' /> 48 <col align='right' /> 49 </colgroup> 50 <tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;"> 51 <th>客戶端及版本</th> 52 <th>通過率</th> 53 <th>開始時間</th> 54 <th>結束時間</th> 55 </tr> 56 %(table_total)s 57 58 </table> 59 <!-- 執行模塊 --> 60 <p class='attribute'><strong>測試報告詳情 : </strong> </p> 61 <table id='result_table' class="table table-condensed table-bordered table-hover"> 62 <colgroup> 63 <col align='left' /> 64 <col align='right' /> 65 <col align='right' /> 66 <col align='right' /> 67 <col align='right' /> 68 </colgroup> 69 <tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;"> 70 <th colspan="2">業務模塊</th> 71 <th>用例總數</th> 72 <th>通過數</th> 73 <th>狀態</th> 74 </tr> 75 %(table_module)s 76 %(table_case)s 77 78 79 </table> 80 <script type="text/javascript"> 81 //change color 82 //取都用demo的多組 83 var eles = document.getElementsByClassName('demo'); 84 console.log(eles); 85 var x=document.getElementById("demo").innerText; 86 console.log("the value is :"+x); 87 //每組都應用樣式 88 for(var i = 0; i < eles.length; i++){ 89 if(eles[i].innerText == 'PASS'){ 90 eles[i].style.color = 'green'; 91 }else{ 92 eles[i].style.color = 'red'; 93 } 94 } 95 96 </script> 97 98 </body> 99 </html>""" 100 # variables: (title,total, table_total,table_module,table_case) 101 102 # ------------------------------------------------------------------------ 103 # Report 104 105 #總數據 106 REPORT_TMPL_TOTAL = """ 107 <tr class='failClass warning'> 108 <td>%(version)s</td> 109 <td>%(radio)s</td> 110 <td>%(runstarttime)s</td> 111 <td>%(runstoptime)s</td> 112 </tr>""" 113 114 #詳情表頭 115 REPORT_TMPL_MODULE = """ 116 <tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;"> 117 <th>%(name)s</th> 118 <th>%(module)s</th> 119 <th>%(casetotal)s</th> 120 <th>%(passtotal)s</th> 121 <th class='demo' id="demo">%(status)s</th> 122 </tr>""" 123 124 #case數據 125 REPORT_TMPL_CASE = """ 126 <tr class='failClass warning'> 127 <td>%(name)s</td> 128 <td>%(module)s</td> 129 <td>%(casetotal)s</td> 130 <td>%(passtotal)s</td> 131 <td class='demo' id="demo2">%(status)s</td> 132 </tr>""" 133 134 135 136 if __name__ == '__main__': 137 table_tr0 = '' 138 table_tr1="" 139 table_tr2="" 140 141 numfail = 1 142 numsucc = 9 143 html = Template_mixin() 144 145 #總表數據 146 table_td = html.REPORT_TMPL_TOTAL % dict(version=VERSION_DICT['version'],radio=VERSION_DICT['radio'],runstarttime=VERSION_DICT['runstarttime'],runstoptime = VERSION_DICT['runstoptime']) 147 table_tr0 += table_td 148 149 #詳情數據 150 table_td_module=html.REPORT_TMPL_MODULE % dict(name="",module=case1["name"],casetotal=case1["total"],passtotal=case1["passnum"],status=case1["status"],) 151 table_tr1 += table_td_module 152 #title 153 title_str="自動化測試報告" 154 #表頭總數 155 total_str = '共 %s,通過 %s,失敗 %s' % (numfail + numsucc, numsucc, numfail) 156 157 #case數據 158 table_td_case=html.REPORT_TMPL_CASE % dict(name="",module=case2["name"],casetotal=case2["total"],passtotal=case2["passnum"],status=case2["status"],) 159 table_tr2 += table_td_case 160 161 output=html.HTML_TMPL % dict(title=title_str,total = total_str,table_total = table_tr0,table_module=table_tr1,table_case=table_tr2) 162 163 # 生成html報告 164 filename='{date}_TestReport.html'.format(date=time.strftime('%Y%m%d%H%M%S')) 165 166 print(filename) 167 #獲取report的路徑 168 dir= os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'report/html') 169 filename=os.path.join(dir,filename) 170 171 with open(filename, 'wb') as f: 172 f.write(output.encode('utf8'))
下一個版本,想要實現caselist批量生成表格。