解析python数据后用html输出

哥们做android自动化测试,可是无奈报告输出字段不是自己想要的,于是想自己解析测试报告,所以想了个方法,还不完善记录分享一下

 
#-*-coding:utf-8-*- 

import json,csv,sys,re
#import xlrd,xlwt
#from xlutils.copy import copy
reload(sys)
sys.setdefaultencoding("utf-8")

f = file(r"result.json")#获取python数据
    #f1 = open(r"D:\result.txt","w")
#a = json.load(f)
class Test(object):
    
    def __init__(self,_file):
        self._file = _file
        
    #获取json
    def get_json(self):
        self.value  = json.load(self._file )
        return self.value
    #使用相应方法获取相应字段
    def get_result(self,value):
        results = self.value["results"]
        key_list = []
        value_list = []
        status_list = []
        key_list = []
        fail_list = []
        #遍历key与value
        try:
            for key,value in results.items():
                #key_list.append(key)
                model = value['deviceDetails']['model']
                key_list.append(model)
                #key_list.append(key)
            
                testR= value["testResults"]
                #print testR
                method_list  = []
                s_list = []
                fails = []
                c_list = []
                for r in testR:
                    method_list.append(r[0]['methodName'])
                    s_list.append(r[1]['status'])
                    if r[1]['status'] == 'FAIL':
                        fail=r[0]['methodName'].strip()
                        try:
                            mess=r[1]['exception']['cause']['message'].strip()
                            fails.append(str(u'FAIL:')+fail+","+"<br/>CASE:"+mess+"<br/>")
                        except:
                            mess = "no message"
                            fails.append(u'FAIL:'+fail+","+"\n CASE:"+mess+"<br/>")
                value_list.append(method_list)
                status_list.append(s_list)
                fail_list.append(fails)
        
            return key_list,value_list,status_list,fail_list
            
        except:
            return 'fail'
#a= Test(f)
#print a.get_result(a.get_json())
#k,v,s,fail= a.get_result(a.get_json())
#print k,v,s,fail    

#输出为html
def html_table():
    a= Test(f)
    #k,v= a.get_result(a.get_json())
    k,v,s,fail= a.get_result(a.get_json())
    print str(v)
    html = '''
        <html>
        <head>
        <style> 
        .table-b table,th, td
  {
  font-size:1em;
  border:1px solid #98bf21;
  padding:3px 7px 2px 7px;
  }
  table
  {
  border-collapse:collapse;
  }
th
  {
  font-size:1.1em;
  text-align:left;
  padding-top:5px;
  padding-bottom:4px;
  background-color:#A7C942;
  color:#ffffff;
  
  }
        </style>
        <body>
        <div class = "table-b">
        <table border = "0">
                <tr>
                        <th>model</th>
                        <th>method</th>
                        <th>cout</th>
                        <th>FAIL</th>
                        <th>PASS</th>
                        <th>FAIL's mehtod</th>
                </tr>'''
    #c = []
   
    for i in range(len(k)):
        #k[i] = "".join(k[i])
        #print len(v)
        #pass
        html += '''
                <tr>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                </tr>
                ''' % (k[i],";".join(v[i]),len(v[i]),s[i].count('FAIL'),s[i].count('PASS'),"".join(fail[i]))
                            
        
    html += '''
        </table>
        </body>
        </html>
        '''
    return html 
    

def report():
    with open('test.html', 'w') as f:
        f.write(html_table())
        
report()

 

 重新对脚本进行了优化 ,对数据进行分行,刚开始怎么都传不开,结果发现输出的是html格式,直接用<br/>就可以解决如此简单的问题啊

posted @ 2015-09-10 15:42  虫子宴  阅读(4797)  评论(0编辑  收藏  举报