Pytest - 问题记录
问题1:使用pytest-xdsit 插件运行后 logging 模块日志不会输出的问题
背景:自己写的日志打印模块,用pytest -n=auto 后日志就不会输出
# tools.set_loggging.py
import logging.handlers
import sys
from concurrent_log_handler import ConcurrentRotatingFileHandler
from config import BASE_PATH
def set_log(log_name):
# 1. 创建日志器对象
logger = logging.getLogger()
# 2. 设置日志打印级别
logger.setLevel(logging.INFO)
# 3. 创建处理器对象
# 输出到控制台
st = logging.StreamHandler()
# 输出到日志文件
# when 是一个字符串,定义了日志切分的间隔时间单位
# interval 是间隔时间单位的个数,指等待多少个 when 的时间后继续进行日志记录
# backupCount 是保留日志的文件个数
fh = logging.handlers.TimedRotatingFileHandler(BASE_PATH + '/logs/' + log_name,
when='midnight',
interval=1,
backupCount=3,
encoding='utf-8')
# 4. 创建格式化器
fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
formatter = logging.Formatter(fmt)
# 5. 给处理器设置格式化器
st.setFormatter(formatter)
fh.setFormatter(formatter)
# 6. 给日志器添加处理器
logger.addHandler(st)
logger.addHandler(fh)
def set_current_log(log_name):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s")
if not logger.handlers: # 判断是否有handlers,有直接记录日志, 不需要创建多个句柄,否则会有日志重复打印问题
# ConcurrentRotatingFileHandler
rotateHandler = ConcurrentRotatingFileHandler(BASE_PATH + '/logs/' + log_name, maxBytes=512 * 1024, backupCount=10,
encoding='utf-8')
rotateHandler.setLevel(logging.INFO)
rotateHandler.setFormatter(formatter)
logger.addHandler(rotateHandler)
# stream_handler = logging.StreamHandler()
# stream_handler.setLevel(logging.INFO)
# stream_handler.setFormatter(formatter)
# logger.addHandler(stream_handler)
解决办法:
脚本路径下或者项目跟路径下添加conftest.py 再初始化日志
参考:https://blog.csdn.net/angl129/article/details/110539640
https://blog.csdn.net/redrose2100/article/details/118271831?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166981490116782425163055%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=166981490116782425163055&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-17-118271831-null-null.nonecase&utm_term=Pytest&spm=1018.2226.3001.4450https://blog.csdn.net/chenlibao0823/article/details/126179538?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166981504116782414915501%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=166981504116782414915501&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-1-126179538-null-null.nonecase&utm_term=%E4%BD%BF%E7%94%A8&spm=1018.2226.3001.4450
问题2: 使用pytest-xdist 和 pytset-testreport 插件生成多个报告:
解决:pytset-allure 代替 pytset-testreport
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/16939712.html