• python 生成HTmL报告页面 V1.3 修改字体颜色


    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'))
    View Code

     下一个版本,想要实现caselist批量生成表格。

  • 相关阅读:
    linq教程
    linq 多表分组查询统计
    System.Diagnostics.Trace.Listeners
    linq多表join与group
    LINQ的左连接、右连接、内连接
    linq pad
    开源项目
    linq group join
    OWIN OAuth 2.0 Authorization Server
    autofac + owin + webform + mvc + webapi集成demo
  • 原文地址:https://www.cnblogs.com/lisa2016/p/10735550.html
Copyright © 2020-2023  润新知