day12_框架一main.py代碼
阿新 • • 發佈:2018-03-21
amp lac 執行 次數 生成 span 代碼 body req
import time
from lib import tools
from lib import report
from lib import sendmail
from conf.settings import EMAIL_INFO,EMAIL_CONTENT_FORMAT
class Main():
def __init__(self,case_path,host):
self.case_path = case_path
self.host = host
def __run(self):
start_time = time.time() # 開始時間
self.all_res_list = [] # 存放所有用例的返回結果,裏面有多個字典
case_list = tools.readCase(self.case_path)
self.pass_count = 0 # 記錄用例的通過次數
for case in case_list: # case是excel裏的每條用例,一行
res_dic = {} # 存放每條用例的執行結果
project = case[0] # 項目名稱
model = case[1] # 模塊
case_id = case[2] # 用例id
detail = case[3] # 用例描述
req_url = self.host+case[4] # 請求url
method = case[5] # 請求方法
req_data = case[6] # 請求數據
hope = case[7] # 預期結果
tester = case[8] # 測試人員
response = tools.my_request(method,req_url,req_data)
req_param = req_url + ‘?‘ + req_data.replace(‘,‘,‘&‘) # 請求報文
case_status = tools.check_res(response,hope) # 用例執行結果
if case_status == ‘通過‘:
self.pass_count += 1
res_dic = {
"case_id": case_id,
"project": project,
"model": model,
"detail": detail,
"url": req_url,
"tester": tester,
"status": case_status,
"request": req_param,
"response": response
}
self.all_res_list.append(res_dic)
end_time = time.time() # 結束時間
self.run_time = end_time - start_time # 開始時間減去結束時間就是運行的時間
def __result(self):
tools.write_excel(self.case_path,self.all_res_list) # 反寫excel
all_case_num = len(self.all_res_list) # 總共多少用例
fail_count = all_case_num - self.pass_count # 失敗的用例
all = {
"all": all_case_num, # 總共多少條用例
"ok": self.pass_count, # 通過的
"fail": fail_count, # 失敗
"run_time": ‘%.2f‘ % self.run_time, # 運行了多久
"case_res": self.all_res_list,
"date": time.strftime(‘%Y/%m/%d %H:%M:%S‘) # 什麽時候執行的
}
my_report = report.HtmlReport(all)
my_report.report() # 生成html測試報告
report_file_name = my_report.file_name # 生成報告的文件名
title = ‘%s接口測試報告‘ % time.strftime(‘%Y/%m/%d %H:%M:%S‘)
content = EMAIL_CONTENT_FORMAT.format(
‘本次共執行%s條用例,通過%s條,失敗%s條。詳情見附件測試報告。‘ % (all_case_num,
self.pass_count, fail_count))
mail_man = sendmail.SendMail(
username = EMAIL_INFO.get(‘username‘),
passwd = EMAIL_INFO.get(‘password‘),
recv = EMAIL_INFO.get(‘recv‘),
email_host = EMAIL_INFO.get(‘email_host‘),
port = EMAIL_INFO.get(‘port‘),
ssl = EMAIL_INFO.get(‘ssl‘),
title = title,
content = content,
file = report_file_name
)
mail_man.send_mail()
def main(self):
self.__run()
self.__result()
if __name__ == ‘__main__‘:
pass
day12_框架一main.py代碼