python 生成HTmL报告页面 V1.2

上代码

  1 # -*- coding=utf-8 -*-
  2 import  time,os
  3  
  4 #数据部分
  5 func_dict={"funcname":"模块1",}
  6 
  7 funcname=['书架','书城','分类','我的']
  8 case1={"name":"模块1","total":"10","passnum":"10","failnum":"0","radio":"80","status":"PASS"}
  9 case2={"name":"模块2","total":"20","passnum":"15","failnum":"5","radio":"75","status":"Fail"}
 10 
 11 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')}
 12 
 13  
 14 class Template_mixin(object):
 15     """html报告"""
 16     HTML_TMPL = r"""
 17         <!DOCTYPE html>
 18         <html lang="en">
 19         <head>
 20             <meta charset="UTF-8">
 21             <title>自动化测试报告</title>
 22             <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
 23             <h2 style="font-family: Microsoft YaHei">自动化测试报告</h2>
 24             <p class='attribute'><strong>测试结果 : </strong> %(value)s</p>
 25             <style type="text/css" media="screen">
 26         body  { font-family: Microsoft YaHei,Tahoma,arial,helvetica,sans-serif;padding: 20px;}
 27         </style>
 28         </head>
 29         <body>
 30             <table id='result_table' class="table table-condensed table-bordered table-hover">
 31                 <colgroup>
 32                     <col align='left' />
 33                     <col align='right' />
 34                     <col align='right' />
 35                     <col align='right' />
 36                 </colgroup>
 37                 <tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
 38                     <th>客户端及版本</th>
 39                     <th>通过率</th>
 40                     <th>开始时间</th>
 41                     <th>结束时间</th>
 42                 </tr>
 43                 %(table_tr)s
 44             <!-- 执行模块 -->
 45             <p class='attribute'><strong>测试详情 : </strong> 执行结果</p>
 46             <table id='result_table' class="table table-condensed table-bordered table-hover">
 47                 <colgroup>
 48                    <col align='left' />
 49                    <col align='right' />
 50                    <col align='right' />
 51                    <col align='right' />
 52                    <col align='right' />
 53                 </colgroup>
 54                 <tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
 55                     <th colspan="2">业务模块</th>
 56                     <th>用例总数</th>
 57                     <th>通过数</th>
 58                     <th>状态</th>
 59                 </tr>
 60                 %(table_tr2)s
 61                 %(table_tr3)s
 62                 
 63  
 64             </table>
 65         </body>
 66         </html>"""
 67     #总数据
 68     TABLE_TMPL_TOTAL = """
 69         <tr class='failClass warning'>
 70             <td>%(version)s</td>
 71             <td>%(radio)s</td>
 72             <td>%(runstarttime)s</td>
 73             <td>%(runstoptime)s</td>
 74         </tr>"""
 75     #详情表头
 76     TABLE_TMPL_MODULE = """
 77         <tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
 78             <th>%(name)s</th>
 79             <th>%(module)s</th>
 80             <th>%(casetotal)s</th>
 81             <th>%(passtotal)s</th>
 82             <th>%(status)s</th>
 83         </tr>"""
 84     #case数据        
 85     TABLE_TMPL_CASE = """
 86         <tr class='failClass warning'>
 87             <td>%(name)s</td>
 88             <td>%(module)s</td>
 89             <td>%(casetotal)s</td>
 90             <td>%(passtotal)s</td>
 91             <td>%(status)s</td>
 92         </tr>"""
 93             
 94 
 95  
 96 if __name__ == '__main__':
 97     table_tr0 = ''
 98     table_tr1=""
 99     table_tr2=""
100 
101     numfail = 1
102     numsucc = 9
103     html = Template_mixin()
104     
105     #总表数据
106     table_td = html.TABLE_TMPL_TOTAL % dict(version=VERSION_DICT['version'],radio=VERSION_DICT['radio'],runstarttime=VERSION_DICT['runstarttime'],runstoptime = VERSION_DICT['runstoptime'])
107     table_tr0 += table_td
108 
109     #详情数据
110     table_td_module=html.TABLE_TMPL_MODULE % dict(name="",module=case1["name"],casetotal=case1["total"],passtotal=case1["passnum"],status=case1["status"],)
111     table_tr1 += table_td_module
112 
113     #表头总数
114     total_str = '共 %s,通过 %s,失败 %s' % (numfail + numsucc, numsucc, numfail)
115    
116     #case数据
117     table_td_case=html.TABLE_TMPL_CASE % dict(name="",module=case2["name"],casetotal=case2["total"],passtotal=case2["passnum"],status=case2["status"],)
118     table_tr2 += table_td_case
119 
120     output=html.HTML_TMPL % dict(value = total_str,table_tr = table_tr0,table_tr2=table_tr1,table_tr3=table_tr2)
121 
122     # 生成html报告
123     filename='{date}_TestReport.html'.format(date=time.strftime('%Y%m%d%H%M%S'))
124  
125     print(filename)
126     #获取report的路径
127     dir= os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),'report')
128     filename=os.path.join(dir,filename)
129  
130     with open(filename, 'wb') as f:
131         f.write(output.encode('utf8'))
View Code

 

posted @ 2019-04-18 19:02  Golover  阅读(5918)  评论(0编辑  收藏  举报