零基础小白 接口自动化测试集锦: https://www.jianshu.com/nb/49125734
日志上篇介绍 https://www.jianshu.com/p/147c4b7ccc11
接口实战--使用log日志
第1步: 新建log目录
新增log目录文件.jpg
第2步: 重构conf.yml配置文件,定义log信息
# -*- coding: utf-8 -*-
# @Time : 2021/1/11 16:02
# @File : conf.yml.py
# @Author : Yvon_₯㎕ζ๓
BASE:
# log等级
log_level: 'debug'
#扩展名
log_extension: ".log"
test:
url: "https://xxxxxxx.com"
case_file: "testdata.xlsx"
case_sheet: "斗地主接口测试"
定义log日志信息.jpg
第3步: 重构Conf.py脚本,定义log文件路径,读取conf.yml中配置的log信息
# -*- coding: utf-8 -*-
# @Time : 2021/1/11 16:16
# @File : Conf.py.py
# @Author : Yvon_₯㎕ζ๓
import os
from utils.YamlUtil import YamlReader
#获取项目基本目录
current = os.path.abspath(__file__)
# print(current)
#获取当前项目的绝对路径
BASE_DIR = os.path.dirname(os.path.dirname(current))
# print(BASE_DIR)
#定义config目录路径
_config_path = BASE_DIR + os.sep + "config"
# print(_config_path)
#定义conf.yml文件路径
_confyml_file = _config_path + os.sep + "conf.yml"
# print(_confyml_file)
# 定义logs文件路径
_log_path = BASE_DIR + os.sep + "logs"
print(_log_path)
#***************************************定义方法**************************************
def get_config_path():
"""
获取config文件夹目录
:return:
"""
return _config_path
def get_confyml_file():
'''
获取conf.yml文件路径目录
:return:
'''
return _confyml_file
def get_log_path():
"""
获取log文件路径
:return:
"""
return _log_path
#读取配置文件,创建类
class ConfigYaml:
def __init__(self):
# 初始化读取yaml配置文件
self.config = YamlReader(get_confyml_file()).data()
# 定义方法获取重要信息
def get_conf_url(self):
'''
获取confyml配置文件中url地址
:return:
'''
return self.config["BASE"]["test"]["url"]
def get_conf_log(self):
"""
获取日志级别
:return:
"""
return self.config["BASE"]["log_level"]
def get_conf_log_extension(self):
"""
获取文件扩展名
:return:
"""
return self.config["BASE"]["log_extension"]
if __name__ == "__main__":
conf_read = ConfigYaml()
print(conf_read.get_conf_log_extension())
print(conf_read.get_conf_log())
定义logs文件路径同时获取路径.jpg
定义方法读取log配置信息.jpg
log脚本运行成功读取log配置信息.jpg
第4步: 封装Log工具类
# -*- coding: utf-8 -*-
# @Time : 2020/10/26 20:15
# @File : LogUtil.py
# @Author : Yvon_₯㎕ζ๓
import logging,datetime,os,sys
from config import Conf
from config.Conf import ConfigYaml
sys.path.append('../') # 新加入的
#封装工具类
#定义日志级别的映射
log_L = {
"info": logging.INFO,
"debug": logging.DEBUG,
"warning": logging.WARNING,
"error": logging.ERROR
}
#1、创建类
class Logger:
#2、定义参数
#输出文件名称,loggername,日志级别
def __init__(self,log_file,log_name,log_level):
self.log_file = log_file # 扩展名 配置文件
self.log_name = log_name # 参数
self.log_level = log_level # 配置文件
#3、编写输出控制台或文件
# 设置logger名称
self.logger = logging.getLogger(self.log_name)
# 设置log级别
self.logger.setLevel(log_L[self.log_level])
# 判断handlers是否存在
if not self.logger.handlers:
# 输出控制台
fh_stream = logging.StreamHandler()
fh_stream.setLevel(log_L[self.log_level]) # logging.INFO
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s ')
fh_stream.setFormatter(formatter)
# 写入文件
fh_file = logging.FileHandler(self.log_file)
fh_file.setLevel(log_L[self.log_level])
fh_file.setFormatter(formatter)
# 添加handler
self.logger.addHandler(fh_stream)
self.logger.addHandler(fh_file)
#1、初始化参数数据
#日志文件名称,日志文件级别
#日志文件名称 = logs目录 + 当前时间+扩展名
#log目录
log_path = Conf.get_log_path()
print(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)
# print(logfile)
#日志文件级别
loglevel = ConfigYaml().get_conf_log()
# print(loglevel)
#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().debug("this is a debug")
工具类log运行结果.jpg
第5步: 执行登录接口返回日志信息
登录接口调用封装的Request请求,而Request请求调用封装的log请求
from config.Conf import ConfigYaml
from common.header_Base import get_SDK_header,get_params
from utils.RequestsUtil import Request
'''初始化'''
request = Request()
def test_login():
#定义测试数据
conf_y = ConfigYaml() # 加载config.Conf文件中的ConfigYaml类
url_path = conf_y.get_conf_url() # 读取配置文件中的url
url = url_path + "/member/api/mi/login" # 拼接url
#发送Post请求
headers = get_SDK_header()
data = get_params()
r = request.post(url, headers=headers, json=data)
#输出结果
print(r)
if __name__ == "__main__" :
test_login()
Request请求调用封装的log.jpg
接口调用封装的Request.jpg
执行登录接口发送请求日志信息.jpg