标签:测试结果 line 解决 第三方 use pip 写代码 简单 通过
import logging
filename = "../report/test_case_run.log"
logging.basicConfig(level=logging.INFO,
format=‘%(asctime)s %(levelname)s1 %(filename)s [line:%(lineno)d] %(message)s‘,
datefmt=‘%a, %d %b %Y %H:%M:%S‘,
filename=filename,
filemode=‘w‘)
|
pip install requests |
import requests
from untils. log_trace import *
#发送get请求
def get_request(url,data=None,headers=None):
res = requests.get(url=url,data=data,headers=headers)
return res
#发送post请求
def post_request(url,data,headers=None):
res = requests.post(url=url,data=data,headers=headers)
return res
#发送delete请求
def del_request(url,data=None,headers=None):
res = requests.delete(url,data=data)
return res
#发送put请求
def put_request(url,data,headers=None):
pass
def send_request(method,url,data=None,headers=None):
try:
logging.info(headers)
if headers:
if method == "GET":
return get_request(url,data,headers=headers)
if method == "POST":
return post_request(url,data=data,headers=headers)
if method == "DELETE":
return del_request(url,data=data,headers=headers)
#put使用频率低,暂时不写
if method == "PUT":
return put_request(url,data=data,headers=headers)
else:
logging.info("Header is null")
except Exception as e:
logging.info("send request fail:%s"%e)
|
#coding:utf-8
from untils.send_request import send_request
def test_send_request():
url="http://127.0.0.1:9000/articles/"
headers = {
"X-Token":"0a6db4e59c7fff2b2b94a297e2e5632e"
}
res = send_request("GET",url,headers=headers)
print(res.json())
if __name__ == "__main__":
test_send_request()
|
/usr/bin/python3.6 /home/stephen/IdeaProjects/apitest/untils/untils_test.py
{‘status‘: ‘BS.200‘, ‘all_titles‘: {‘amy1‘: ‘alive‘, ‘modifytest‘: ‘alive‘, ‘addTest‘: ‘alive‘}, ‘msg‘: ‘query articles sucess.‘}
Process finished with exit code 0
|
pip install xlrd
pip install xluntils
|
#coding:utf-8
import xlrd
from untils.log_trace import *
from xlutils.copy import copy
from untils.load_conf import excel_config
class excel_tool():
def __init__(self,excel_name):
self.curr_excel = xlrd.open_workbook(excel_name)
self.table = self.curr_excel.sheet_by_index(0)
#print(self.table.cell(1,1).value)
#实例化excel_config
self.config = excel_config()
self.rows = self.table.nrows
self.excel_name = excel_name
#获取用例编号
def get_caseno(self,row):
caseno = self.table.cell(row,self.config.caseno_col()).value
if caseno:
return caseno
else:
logging.info("case no is null")
return None
#获取用例名称
def get_casename(self,row):
casename = self.table.cell(row,self.config.casename_col()).value
return casename
#获取是否运行标志
def get_runflag(self,row):
run_flag = self.table.cell(row,self.config.isrun_col()).value
return run_flag
#获取用例级别
def get_caselevel(self,row):
caselevel = self.table.cell(row,self.config.level_col()).value
return caselevel
#获取请求url
def get_caseurl(self,row):
caseurl = self.table.cell(row,self.config.caseurl_col()).value
return caseurl
#获取请求body
#获取请求body
def get_casebody(self,row):
case_body = self.table.cell(row,self.config.casebody_col()).value
return case_body
#获取header
def get_headerflag(self,row):
headerflag = self.table.cell(row,self.config.header_col()).value
return headerflag
#获取coocikes
def get_cookiesflag(self,row):
cookiesflag = self.table.cell(row,self.config.cookies_col()).value
return cookiesflag
#获取请求类型
def get_methodtype(self,row):
method_type = self.table.cell(row,self.config.reqtype_col()).value
return method_type
#获取预期结果
def get_expectres(self,row):
expect_res = self.table.cell(row,self.config.expectresult_col()).value
return expect_res
#获取测试结果
def get_testres(self,row):
test_res= self.table.cell(row,self.config.testresult_col()).value
return test_res
#获取操作符
def get_operator(self,row):
operator = self.table.cell(row,self.config.test_operator_col()).value
return operator
#回写测试结果到excel
def write_testres(self,row,value):
wbook = copy(xlrd.open_workbook(self.excel_name))
sheet = wbook.get_sheet(0)
sheet.write(row, self.config.testresult_col(), value)
wbook.save(self.excel_name)
#回写实际结果
def write_actualres(self,row,value):
wbook = copy(xlrd.open_workbook(self.excel_name))
sheet = wbook.get_sheet(0)
sheet.write(row, self.config.actualresult_col(), value)
wbook.save(self.excel_name) |
#coding:utf-8
from untils.excel_tool import excel_tool
from untils.send_request import send_request
from untils.log_trace import *
from untils.check_result import CheckResult
import json
headers = {
"X-Token":"0a6db4e59c7fff2b2b94a297e2e5632e"
}
class runner():
def __init__(self):
self.excel = excel_tool("../testcase/test.xls")
self.check = CheckResult()
def join_case(self):
global skip_list,sucess_list,failed_list,skip_list
sucess_list = []
sucess_list = []
failed_list = []
skip_list = []
for row in range(1,self.excel.rows):
no = self.excel.get_caseno(row)
url = self.excel.get_caseurl(row)
isrun = self.excel.get_runflag(row)
name = self.excel.get_casename(row)
level = self.excel.get_caselevel(row)
data = self.excel.get_casebody(row)
expect_res = self.excel.get_expectres(row)
method = self.excel.get_methodtype(row)
hasheader = self.excel.get_headerflag(row)
operator = self.excel.get_operator(row)
if isrun == "Y":
logging.info("Begin to run test case : %s,case number :%s" %(name,no))
logging.info("Request method type is :%s" %method)
logging.info("Request URL:%s" %url)
logging.info("Request Body:%s" %json.dumps(json.loads(data),sort_keys=True,indent=2))
res = send_request(method,url,data=data,headers=headers)
is_sucess = self.check.cmpdict(eval(expect_res),eval(res.text),operator)
print(is_sucess)
if is_sucess:
sucess_list.append(name)
#回写测试结果
self.excel.write_testres(row,"pass")
#回写实际结果
self.excel.write_actualres(row,res.text)
logging.info("Test case %s run sucess." %name)
else:
failed_list.append(name)
print("fail",is_sucess)
#回写测试结果
self.excel.write_testres(row,"failed")
#回写实际结果
self.excel.write_actualres(row,res.text)
logging.error("Test case %s run fail." %name)
logging.info("Response is:%s" %json.dumps(res.json(),sort_keys=True,indent=2))
else:
skip_list.append(name)
self.excel.write_testres(row,"skipped")
def sum(self):
total = len(sucess_list)+len(failed_list) + len(skip_list)
failed = len(failed_list)
sucess = len(sucess_list)
logging.info("-----------------------------------------------------------")
logging.info("本次一共运行:%s 个用例" %total)
logging.info("本次运行通过:%s 个用例" %sucess)
logging.info("本次运行跳过:%s 个用例" %len(skip_list))
logging.info("跳过的用例:%s" %skip_list)
logging.info("-----------------------------------------------------------")
|
logging.error("%s is not equal to %s" %(expect[key],self.dict_value(key,actual)))
return False
if equal == "notequal":
for key in expect.keys():
if key != self.dict_value(key,actual):
logging.info("%s is not equal to %s" %(expect[key],self.dict_value(key,actual)))
return True
else:
logging.error("%s is equal to %s" %(expect[key],self.dict_value(key,actual)))
return False
else:
logging.error("Operator :%s is not support now,you can define it in file[check_result.py]" %equal)
else:
logging.error("Expect or actual result is not dict,check it in excel. ")
|
from untils.run_main import runner
if __name__ == "__main__":
#test_send_request()
runner = runner()
runner.join_case()
runner.sum()
|
标签:测试结果 line 解决 第三方 use pip 写代码 简单 通过
原文地址:https://www.cnblogs.com/bzdmz/p/10934173.html