def run_api(self, url, params, method='get', header=None, cookie=None): """ 发送请求 """ request = Request(ConfigYaml().get_config_url()) if not len(str(params).strip()): return params params = json.loads(params) if str(method).lower() == "get": res = request.get(url, json=params, headers=header, cookies=cookie) log.debug('get请求') elif str(method).lower() == "post": res = request.post(url, json=params, headers=header, cookies=cookie) log.debug('post请求') else: log.error(f"错误的请求方法:{method}") return res
def send_mail(report_html_path="", content="", title="测试"): """ 发送邮件 :param report_html_path: :param content: :param title: :return: """ email_info = ConfigYaml().get_email_info() smtp_address = email_info['smtpserver'] username = email_info['username'] password = email_info['password'] receiver = email_info['receiver'] email = SendEmail(smtp_address=smtp_address, username=username, password=password, receiver=receiver, title=title, content=content, file=report_html_path) email.send_mail()
def init_db(db_alias): # db_1: # db_host: "127.0.0.1" # bd_user: "******" # db_password: "******" # bd_database: "test_27" # db_charset: "utf8" # db_port: "3306" #初始化数据信息 通过配置 db_info = ConfigYaml().get_db_conf_info(db_alias) host = db_info["db_host"] user = db_info["bd_user"] password = db_info["db_password"] database = db_info["bd_database"] charset = db_info["db_charset"] port = int(db_info["db_port"]) #port需要转为int类型 #初始化mysql对象 conn = Mysql(host, user, password, database, charset, port) print(conn) return conn
def send_mail(report_html_path="", content="", title="测试"): """ 发送邮件 :param report_html_path: :param content: :param title: :return: """ email_info = ConfigYaml().get_email_info() smtp_addr = email_info["smtpserver"] username = email_info["username"] password = email_info["password"] recv = email_info["receiver"] email = SendEmail(smtp_addr=smtp_addr, username=username, password=password, recv=recv, title=title, content=content, file=report_html_path) email.send_mail()
def run_pre(self, pre_case): url = ConfigYaml().get_conf_url() + pre_case[data_key.url] method = pre_case[data_key.method] params = pre_case[data_key.params] headers = pre_case[data_key.headers] cookies = pre_case[data_key.cookies] # 1.判断headers是否存在,存在:json转义 不存在:无需作任何操作 # if headers: # header = json.loads(headers) # else: # header = headers header = json_parse(headers) # 3.判断cookies是否存在,存在:json转义 不存在:无需作任何操作 # if cookies: # cookie = json.loads(cookies) # else: # cookie = cookies cookie = json_parse(cookies) res = self.run_api(url, method, params, header) print("前置用例执行:%s" % res) return res
def send_mail(report_html_path='',content = '',title = "测试报告邮件"): email_info = ConfigYaml().get_email_info() smtp_addr = email_info["smtpserver"] username = email_info["username"] password = email_info["password"] sender = email_info["sender"] to_receiver = email_info["to_receiver"] cc_receiver = email_info["cc_receiver"] recv = to_receiver.split(',') + cc_receiver.split(',') # print(recv) email = SendEmail( smtp_addr = smtp_addr, username = username, password = password, sender = sender, recv = recv, title = "测试报告邮件", content = content, file = report_html_path ) email.send_mail()
def test_run(self): data_key = ExcelConfig.DataConfig # run_list第一个用例,根据key获取values url = ConfigYaml().get_conf_url() + run_list[0][data_key.url] # print(url) case_id = run_list[0][data_key.case_id] case_model = run_list[0][data_key.case_model] case_name = run_list[0][data_key.case_name] pre_exec = run_list[0][data_key.pre_exec] method = run_list[0][data_key.method] params_type = run_list[0][data_key.params_type] params = run_list[0][data_key.params] expect_result = run_list[0][data_key.expect_result] headers = run_list[0][data_key.headers] cookies = run_list[0][data_key.cookies] status_code = run_list[0][data_key.status_code] db_verify = run_list[0][data_key.db_verify] # 2).根据url发送接口请求 request = Request() # params 获取的是字符类型 需要转义成json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) # method: post / get if str(method).lower() == "get": # 2.增加headers res = request.get(url, json=params) elif str(method).lower() == "post": res = request.post(url, json=params) else: log.error("错误请求method:%s" % method) print(res) # TestExcel().test_run()
from config import Conf from common.ExcelData import Data from common.ExcelConfig import DataConfig from common import ExcelConfig from common import Base from utils.RequestsUtil import Request from utils.AssertUtil import AssertUtil from utils.LogUtil import my_log import os, json, pytest import allure #1、初始化信息 #1).初始化测试用例文件 case_file = os.path.join(Conf.get_data_path(), ConfigYaml().get_excel_file()) # 拼接路径+文件 # print(case_file) #2).测试用例sheet名称 sheet_name = ConfigYaml().get_excel_sheet() # print(sheet_name) #3).获取运行测试用例列表 data_init = Data(case_file, sheet_name) run_list = data_init.get_run_data() print(run_list) # print(json.dumps(run_list, sort_keys=True, ensure_ascii=False, indent=4, separators=(', ', ': '))) # Json格式打印 #4).日志 log = my_log()
sys.path.append('../') import pytest import json import re import allure from config import Conf from config.Conf import ConfigYaml from common.ExcelData import Data from utils.LogUtil import my_log from common import ExcelConfig from common import Base from utils.RequestsUtil import Request from utils.AssertUtil import AssertUtil case_file = os.path.join(Conf.get_data_path, ConfigYaml().get_excel_file()) sheet_name = ConfigYaml().get_excel_sheet() data_init = Data(case_file, sheet_name) run_list = data_init.get_run_data() log = my_log() # 初始化dataconfig data_key = ExcelConfig.DataConfig # 一个用例运行 # 1)初始化信息, url, data # 2) 接口请求 class TestExcel(): # 1. 增加pytest
# 创建输出到文件的handler fh_file = logging.FileHandler(self.log_file) fh_file.setLevel(log_l[log_level]) fh_file.setFormatter(self.formatter) # 添加handler self.logger.addHandler(fh_stream) self.logger.addHandler(fh_file) """ 定义对外的方法,参数只有log_name """ # 初始化参数数据 log_path = Conf.get_log_path() # log目录路径 current_time = datetime.datetime.now().strftime("%Y-%m-%d") # 当前年月日 log_extension = ConfigYaml().get_conf_log_extension() # .log扩展名 logfile = os.path.join(log_path, current_time + log_extension) # 拼接log文件的路径 loglevel = ConfigYaml().get_conf_log_level() # log等级 # 对外方法 def my_log(log_name=__file__): return Logger(log_name=log_name, log_file=logfile, log_level=loglevel).logger if __name__ == '__main__': my_log().info("hello word")
def get_indicator_data(response_data): conf_indicator_list = ConfigYaml().get_titan_info() for indicator_name in conf_indicator_list: test_indicator_list = list() file = Conf.get_titan_indicator_path( ) + os.sep + indicator_name + ".txt" with open(file, "r", encoding="utf-8") as f: standard_indicator_list = eval(f.read()) for first_data in response_data: disease_title = first_data["title"] if disease_title == indicator_name: if "children" in first_data and first_data["children"]: for second_data in first_data["children"]: if second_data["title"] is None: second_data["title"] = " " primary_classification_title = second_data["title"] if "children" in second_data and second_data[ "children"]: for third_data in second_data["children"]: if third_data[ "title"] is None or "title" not in third_data.keys( ): secondary_classification_title = " " else: secondary_classification_title = third_data[ "title"] if "children" in third_data and third_data[ "children"]: for fourth_data in third_data[ "children"]: if "title" not in fourth_data.keys( ): fourth_data["title"] = " " elif fourth_data["title"] is None: fourth_data["title"] = " " tertiary_classification_title = fourth_data[ "title"] if "children" in fourth_data and fourth_data[ "children"]: for fifth_data in fourth_data[ "children"]: if "title" not in fifth_data.keys( ): fifth_data[ "title"] = " " elif fifth_data[ "title"] is None: fifth_data[ "title"] = " " fourth_classification_title = fifth_data[ "title"] if "children" in fifth_data and fifth_data[ "children"]: for sixth_data in fifth_data[ "children"]: if "title" not in sixth_data.keys( ): sixth_data[ "title"] = " " elif sixth_data[ "title"] is None: sixth_data[ "title"] = " " fifth_classification_title = sixth_data[ "title"] if "children" in sixth_data and sixth_data[ "children"]: for seventh_data in sixth_data[ "children"]: if seventh_data[ "title"] is None: seventh_data[ "title"] = " " seventh_classification_title = seventh_data[ "title"] title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title + " -- " + fourth_classification_title + " -- " + fifth_classification_title + " -- " + seventh_classification_title test_indicator_list.append( title) else: title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title + " -- " + fourth_classification_title + " -- " + fifth_classification_title test_indicator_list.append( title) else: title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title + " -- " + fourth_classification_title test_indicator_list.append( title) else: title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title + " -- " + tertiary_classification_title test_indicator_list.append( title) else: title = disease_title + " -- " + primary_classification_title + " -- " + secondary_classification_title test_indicator_list.append(title) Assertions().assert_in_titan_data(standard_indicator_list, test_indicator_list, indicator_name)
def __init__(self): self.url = ConfigYaml().get_conf_url() self.log = my_log("Requests") self.Assert = AssertUtil()
def test_run(self, case): # print("-----------------",case) # 重构函数内容 # data_key = ExcelConfig.DataConfig #run_list 第1个用例,key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print("----------", url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cooikes = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] #接口请求 #验证前置条件: if pre_exec: pass #找到执行用例 # 前置测试用例 pre_case = data_init.get_case_pre() print("前置条件信息为%s" % pre_exec) pre_res = self.run_pre(pre_case) headers, cooikes = self.get_correlation(headers, cooikes, pre_res) header = Base.json_parse(headers) cooikes = Base.json_parse(cooikes) res = self.run_api(url, method, params, headers, cooikes) print("测试用例执行:%s" % res) # #接口请求 # request = Request() # #params转义json # if len(str(params).strip()) is not 0: # params = json.loads(params) # #method post、get # if str(method).lower() == "get": # #增加了headers # res = request.get(url, json=params,headers = header,cooikes=cookie) # elif str(method).lower() == "post": # res = request.post(url, json=params,headers = header,cooikes=cookie) # else: # log.error("错误的请求method %s" % method) # print(res) #allure #sheet名称 feature一级标签 allure.dynamic.feature(sheet_name) #模块 story 二级标签 allure.dynamic.story(case_model) #用例ID+接口名称 title allure.dynamic.title(case_id + case_name) #请求url 请求类型 期望结果 实际结果描述 desc = "<font color = 'red'>请求URL:</font>{}<br/>" \ "<font color = 'red'>请求类型:</font>{}<br/>" \ "<font color = 'red'>期望结构:</font>{}<br/>" \ "<font color = 'red'>实际结果:</font>{}".format(url,method,expect_result,res) allure.dynamic.description(desc) # TestExcel().test_run() #断言验证 #状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(code)) #返回结果内容 assert_util.assert_in_body(res["body"], str(expect_result)) #数据库结果断言 #初始化数据库 Base.assert_db("db_1", res("body"), db_verify) from common.Base import init_db
# -*- coding: utf-8 -*- # @Time : 2020/11/23 20:22 # @File : Test_login.py # @Author : Yvon_Fajin from config.Conf import ConfigYaml import pytest from utils.RequestsUtil import Request #1、获取测试用例内容list #获取testlogin.yml文件路径 data_list = ConfigYaml().get_testlogin_conf_info() # print(data_list) @pytest.mark.parametrize("login", data_list) # login 是定义的变量 #2、参数化执行测试用例 def test_yaml(login): #初始化url,data url = ConfigYaml().get_conf_url() + login["url"] # print("url %s"%url) data = login["data"] # print("data %s"%data) #post请求 request = Request() res = request.post(url, json=data) print(res) if __name__ == "__main__":
def test_run(self, case): # data_key = ExcelConfig.DataConfig # run_list第一个用例 url = ConfigYaml().get_conf_url() + case[data_key.url] # print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] # # 判断headers是否存在,json转义 # if headers: # header = json.loads(headers) # else: # header = headers # # 增加cookies # if cookies: # cookie = json.loads(cookies) # else: # cookie = cookies # 验证前置条件 if pre_exec: # 执行测试用例 pre_case = data_init.get_case_pre(pre_exec) # print("前置条件为:%s"%pre_case) pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) header = Base.json_parse(headers) cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("测试用例执行:%s" % res) # allure ''' sheet名称 feature 一级标签 模块 story 二级标签 用例ID+接口名称 title 请求url、请求类型、期望结果、实际结果 描述 ''' allure.dynamic.feature(sheet_name) allure.dynamic.story(case_model) allure.dynamic.title(case_id + case_name) desc = "<font color='red'> 请求URL:</font> {} <Br/> " \ "<font color='red'> 请求类型:</font> {} <Br/> " \ "<font color='red'> 期望结果:</font> {} <Br/> " \ "<font color='red'> 实际结果:</font> {} ".format(url,method,expect_result,res) allure.dynamic.description(desc) # 断言验证 # 状态码,返回结果内容,数据库相关结果验证 assert_util = AssertUtil() assert_util.assert_code((res['code']), int(code)) # 返回结果内容,body assert_util.assert_in_body(str(res["body"]), str(expect_result)) # 数据库断言 Base.assert_db("db_1", res['body'], db_verify)
def test_run(self, case): # 3. 重构函数内容 # data_key = ExcelConfig.DataConfig # run_list第一个用例,根据key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] status_code = case[data_key.status_code] db_verify = case[data_key.db_verify] # 1.判断headers是否存在,存在:json转义 不存在:无需作任何操作 # if headers: # header = json.loads(headers) # else: # header = headers # header = json_parse(headers) # 3.判断cookies是否存在,存在:json转义 不存在:无需作任何操作 # if cookies: # cookie = json.loads(cookies) # else: # cookie = cookies # cookie = json_parse(cookies) # 1.验证前置条件是否存在 if pre_exec: # 2.根据前置条件找到前置用例 pre_case = data_init.get_case_pre(pre_exec) print("前置条件信息为:%s" % pre_case) pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) header = json_parse(headers) cookie = json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("测试用例执行:%s" % res) # 生成allure测试报告 # sheet名称 feature 一级标签 allure.dynamic.feature(sheet_name) # 模块 story 二级标签 allure.dynamic.story(case_model) # 用例ID+接口名称 title allure.dynamic.title(case_id + case_name) # 请求URL 请求类型 期望结果 实际结果描述 desc = "<font color='red'>请求url: </font>{}<Br/>" \ "<font color='red'>请求类型:</font>{}<Br/>" \ "<font color='red'>期望结果:</font>{}<Br/>" \ "<font color='red'>实际结果:</font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) # 执行allure命令 #断言验证 # 验证状态码 AssertUtil().assert_code(int(res["code"]), int(status_code)) # 验证返回结果内容 AssertUtil().assert_in_body(str(res["body"]), str(expect_result)) # 数据库结果断言 Base.assert_db("db_1", res["body"], db_verify)
def test_run(self): data_key = ExcelConfig.DataConfig url = ConfigYaml().get_conf_url() + run_list[0][data_key.url]0 print(url)
objectName: InsterAutoTest_w fileName: test_case_1 Author: Hang Date: 2020/4/15/015 description: ============================================== """ import os from common.ExcelDate import Data from config.Conf import ConfigYaml from utils.LogUtil import log from common import ExcelConfig # # case_file = os.path.join("../data", ConfigYaml().get_excel_file()) sheet_index = ConfigYaml().get_excel_sheet() run_list = Data("../../data/testdata.xlsx", int(sheet_index)).get_run_data() log = log() class TestExcel: def test_run(self): data_key = ExcelConfig.DataConfig url = ConfigYaml().get_conf_url() + run_list[0][data_key.url]0 print(url) TestExcel().test_run()
def requests_api(self, url, data=None, json=None, params=None, headers=None, cookies=None, method="get"): url = ConfigYaml().get_conf_url() + url login = ConfigYaml().get_login_info() if method == "get": if params is not None: if "access_token" in params: params["access_token"] = login["access_token"] if "authToken" in params: params["authToken"] = login["authToken"] if "userId"in params: params["userId"] = login["userId"] if "authUserId"in params: params["authUserId"] = login["authUserId"] if "operatorId"in params: params["operatorId"] = login["operatorId"] if "hospitalCode"in params: params["hospitalCode"] = login["hospitalCode"] if "authName"in params: params["authName"] = login["authName"] if "uniqu_no"in params: params["uniqu_no"] = login["uniqu_no"] # get请求 self.log.debug("发送get请求") r = requests.get(url, params=params, headers=headers, cookies=cookies) elif method == "post": if data is not None: if "access_token" in data: data["access_token"] = login["access_token"] if "authToken" in data: data["authToken"] = login["authToken"] if "userId" in data: data["userId"] = login["authUserId"] if "authUserId" in data: data["authUserId"] = login["authUserId"] if "operatorId" in data: data["operatorId"] = login["operatorId"] if "hospitalCode" in data: data["hospitalCode"] = login["hospitalCode"] if "authName" in data: data["authName"] = login["authName"] if "uniqu_no" in data: data["uniqu_no"] = login["uniqu_no"] elif json is not None: if "access_token" in json: data["access_token"] = login["access_token"] if "authToken" in json: data["authToken"] = login["authToken"] if "userId" in json: data["userId"] = login["authUserId"] if "authUserId" in json: data["authUserId"] = login["authUserId"] if "operatorId" in json: data["operatorId"] = login["operatorId"] if "hospitalCode" in json: data["hospitalCode"] = login["hospitalCode"] if "authName" in json: data["authName"] = login["authName"] if "uniqu_no" in json: data["uniqu_no"] = login["uniqu_no"] # post请求 self.log.debug("发送post请求") r = requests.post(url, data=data, json=json, headers=headers, cookies=cookies) # 2. 重复的内容,复制进来 # 获取结果内容 code = r.status_code try: body = r.json() except Exception as e: body = r.text # 内容存到字典 res = dict() res["code"] = code res["body"] = body # 字典返回 return res
def test_run(self, case): #run_list 第一个用例,用例,key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] # print(case_id) case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] code = case[data_key.code] db_verify = case[data_key.db_verify] #动态headers请求 ##1.验证前置条件 if pre_exec: pass ##2.找到执行用例 pre_case = data_init.get_case_pre(pre_exec) print("前置条件信息为: %s" % pre_case) pre_res = self.run_pre(pre_case) headers, cookies = self.get_correlation(headers, cookies, pre_res) header = Base.json_parse(headers) cookie = Base.json_parse(cookies) res = self.run_api(url, method, params, header, cookie) print("测试用例执行: %s" % res) # return res # 注意 # sheet名称--feature 一级标签 allure.dynamic.feature(sheet_name) # 模块--story 二级标签 allure.dynamic.story(case_model) # 用例ID + 接口名称 - - title allure.dynamic.title(case_id + case_name) #请求URL 请求类型 期望结果 实际结果 description # allure.dynamic.description(url+method+expect_result+res) desc = "<font color='green' size=3>请求URL: </font> {}<Br/>" \ "<font color='green' size=3>请求类型: </font>{}<Br/>" \ "<font color='red' size=3>期望结果: </font>{}<Br/>" \ "<font color='green' size=3>实际结果: </font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) #1.断言验证 #2.状态码,返回结果内容,数据库相关结果验证 #"""断言验证--状态码,返回结果内容,数据库相关的结果的验证""" #1.)状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(code)) #2.)返回结果内容验证 assert_util.assert_in_body(str(res["body"]), str(expect_result)) #********数据库结果断言******** Base.assert_db("db_1", res["body"], db_verify)
# -*- coding: utf-8 -*- # @Time : 2020/12/9 19:20 # @File : EmailUtil.py # @Author : Yvon_fajin from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText import smtplib from config.Conf import ConfigYaml from email.header import Header email_info = ConfigYaml().get_email_info() #1.初始化 #2.#smtp地址,用户名,密码,接收邮件者,邮件标题,邮件内容,邮件附件 class SendEmail: def __init__(self,smtp_addr,username,password,sender, recv,title,content=None,file=None): self.smtp_addr = smtp_addr self.username = username self.password = password self.sender = sender self.recv = recv self.title = title self.content = content self.file = file #3.发送邮件方法 def send_mail(self): #MIME msg = MIMEMultipart() #初始化邮件信息
def login(): url = ConfigYaml().get_conf_url_test() + '/authorizations/' data = {"username": "******", "password": "******"} a = Requests() r = a.post(url, json=data) print(r)
from config.Conf import ConfigYaml import os from common.ExcelData import Data from utils.LogUtil import my_log from common import ExcelConfig from utils.RequestsUtil import Request import json import pytest from common.Base import json_parse # 1.初始化信息 # 1).初始化测试用例文件 case_file = os.path.join("../data", ConfigYaml().get_excel_file()) # 2).初始化测试用例sheet名称 sheet_name = ConfigYaml().get_excel_sheet() # 3).获取运行的测试用例列表 data_init = Data(case_file, sheet_name) run_list = data_init.get_run_data() # print(run_list) # 4).初始化日志信息 log = my_log() # 初始化DataConfig data_key = ExcelConfig.DataConfig # 2.编写测试用例方法,实现参数化运行 # 先完成一个测试用例的运行 class TestExcel: #1. 增加Pytest参数化 #2. 修改方法参数 #3. 重构函数内容
def test_run(self,case): # 3、重构函数内容 data_key = ExcelConfig.DataConfig # run_list第1个用例,用例key获取values url = ConfigYaml().get_conf_url() + case[data_key.url] print(url) case_id = case[data_key.case_id] case_model = case[data_key.case_model] case_name = case[data_key.case_name] pre_exec = case[data_key.pre_exec] method = case[data_key.method] params_type = case[data_key.params_type] params = case[data_key.params] expect_result = case[data_key.expect_result] headers = case[data_key.headers] cookies = case[data_key.cookies] status_code = case[data_key.status_code] db_verify = case[data_key.db_verify] # 2).接口请求 request = Request() # params 转义json # 验证params有没有内容 if len(str(params).strip()) is not 0: params = json.loads(params) # headers 转义json # 验证headers有没有内容 if len(str(headers).strip()) is not 0: headers = json.loads(headers) # cookies 转义json # 验证cookies有没有内容 if len(str(cookies).strip()) is not 0: cookies = json.loads(cookies) # method post/get if str(method).lower() == "get": res = request.get(url, data=params, headers=headers, cookies=cookies) elif str(method).lower() == "post": res = request.post(url, data=params, headers=headers, cookies=cookies) else: res = log.error("错误请求method: %s" % method) print(res) # allure # sheet名称 feature 一级标签 allure.dynamic.feature(sheet_name) # 模块 story 二级标签 allure.dynamic.story(case_model) # 用例ID+接口名称 title allure.dynamic.title(case_id + case_name) # 请求URL 请求类型 期望结果 实际结果描述 desc = "<font color='red'>请求URL: </font> {}<Br/>" \ "<font color='red'>请求类型: </font>{}<Br/>" \ "<font color='red'>期望结果: </font>{}<Br/>" \ "<font color='red'>实际结果: </font>{}".format(url, method, expect_result, res) allure.dynamic.description(desc) # 断言验证 # 状态码,返回结果内容,数据库相关的结果的验证 # 状态码 assert_util = AssertUtil() assert_util.assert_code(int(res["code"]), int(status_code)) # 返回结果内容 assert_util.assert_in_body(str(res["body"]), str(expect_result))
# 4、输出到写入文件 fh_file = logging.FileHandler(self.log_file) fh_file.setFormatter(formatter) fh_file.setLevel(log_dict[self.file_log_level]) self.logger.addHandler(fh_stream) self.logger.addHandler(fh_file) # 1、初始化参数数据 # 日志文件名称,日志文件级别 # 日志文件名称 = logs目录 + 当前时间+拓展名 # logs目录 log_path = Conf.get_log_path() current_time = datetime.datetime.now().strftime("%Y-%m-%d") # 拓展名 log_extension = ConfigYaml().get_conf_log_extension() logfile = os.path.join(log_path, current_time + log_extension) loglevel = ConfigYaml().get_conf_log_level() # 2、对外方法,初始化log工具类,提供其他类使用 def my_log(log_name=__file__): return Logger(log_file=logfile, log_name=log_name, log_level=loglevel).logger if __name__ == "__main__": my_log().info("this is a debug test")
def __init__(self, url=ConfigYaml().get_config_url()): self.url = url