哥们做android自动化测试,可是无奈报告输出字段不是自己想要的,于是想自己解析测试报告,所以想了个方法,还不完善记录分享一下
#-*-coding:utf-8-*- import json,csv,sys,re#import xlrd,xlwt#from xlutils.copy import copyreload(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+","+"CASE:"+mess+"") except: mess = "no message" fails.append(u'FAIL:'+fail+","+"\n CASE:"+mess+"") 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 #输出为htmldef 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 = '''''' return html def report(): with open('test.html', 'w') as f: f.write(html_table()) report()
''' #c = [] for i in range(len(k)): #k[i] = "".join(k[i]) #print len(v) #pass html += ''' model method cout FAIL PASS FAIL's mehtod ''' % (k[i],";".join(v[i]),len(v[i]),s[i].count('FAIL'),s[i].count('PASS'),"".join(fail[i])) html += ''' %s %s %s %s %s %s
重新对脚本进行了优化 ,对数据进行分行,刚开始怎么都传不开,结果发现输出的是html格式,直接用<br/>就可以解决如此简单的问题啊