零基礎(chǔ)小白 接口自動化測試集錦: http://www.reibang.com/nb/49125734
日志上篇介紹 http://www.reibang.com/p/147c4b7ccc11
接口實戰(zhàn)--使用log日志
第1步: 新建log目錄
新增log目錄文件.jpg
第2步: 重構(gòu)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步: 重構(gòu)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)
#獲取當(dāng)前項目的絕對路徑
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
#讀取配置文件,創(chuàng)建類
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算柳、創(chuàng)建類
class Logger:
#2外厂、定義參數(shù)
#輸出文件名稱,loggername,日志級別
def __init__(self,log_file,log_name,log_level):
self.log_file = log_file # 擴展名 配置文件
self.log_name = log_name # 參數(shù)
self.log_level = log_level # 配置文件
#3劝赔、編寫輸出控制臺或文件
# 設(shè)置logger名稱
self.logger = logging.getLogger(self.log_name)
# 設(shè)置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丁鹉、初始化參數(shù)數(shù)據(jù)
#日志文件名稱,日志文件級別
#日志文件名稱 = logs目錄 + 當(dāng)前時間+擴展名
#log目錄
log_path = Conf.get_log_path()
print(log_path)
#當(dāng)前時間
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運行結(jié)果.jpg
第5步: 執(zhí)行登錄接口返回日志信息
登錄接口調(diào)用封裝的Request請求,而Request請求調(diào)用封裝的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():
#定義測試數(shù)據(jù)
conf_y = ConfigYaml() # 加載config.Conf文件中的ConfigYaml類
url_path = conf_y.get_conf_url() # 讀取配置文件中的url
url = url_path + "/member/api/mi/login" # 拼接url
#發(fā)送Post請求
headers = get_SDK_header()
data = get_params()
r = request.post(url, headers=headers, json=data)
#輸出結(jié)果
print(r)
if __name__ == "__main__" :
test_login()
Request請求調(diào)用封裝的log.jpg
接口調(diào)用封裝的Request.jpg
執(zhí)行登錄接口發(fā)送請求日志信息.jpg