当前位置: 首页 > news >正文

pytest实现日志按用例输出到指定文件中

场景

执行自动化用例时,希望日志按用例生成一个文件,并且按用例所在文件生成目录,用例失败时便于查看日志记录

实现方式

  • pytest.ini文件
    在pytest.ini配置文件中设置配置项(定义日志输出级别和格式)
log_cli=true
log_cli_level = DEBUG
log_file_date_format = %Y-%m-%d %H:%M:%S
log_file_format = %(asctime)s %(filename)s:%(funcName)s:%(lineno)d  %(levelname)s:%(message)s
  • conftest.py文件
    设置日志文件路径
import logging@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_setup(item):   #pytest_runtest_setup是pytest中hook函数,用例执行前都会执行file = os.path.basename(item.path) # 获取case所在文件logging.info(file)config = item.configlogging_plugin = config.pluginmanager.get_plugin("logging-plugin") #获取logging-plugin,在_pytest/logging.py文件中可以看到此plugin的实现full_fname = os.path.join("logs/", file, item._request.node.name + '.log')  #日志文件路径logging_plugin.set_log_path(full_fname) #设置日志路径yield
  • test_case.py 测试文件
    如下所示编写测试用例, 会在logs文件下生成目录"test_case.py", 并且此目录下会有执行的两个用例的日志文件
import logging
test_data = [{"id": "11111"}, {"id": "2222"}]@pytest.mark.parametrize("data", test_data,ids=["test1", "test2"])
class TestCase:def test_case(self,data):logging.inf("test info")pass
http://www.lryc.cn/news/165096.html

相关文章:

  • 程序员面试逻辑题
  • 自动创建设备节点udev机制实现
  • 目标检测YOLO实战应用案例100讲-基于小样本学习和空间约束的濒危动物目标检测
  • 苹果数据恢复软件:Omni Recover Mac
  • 树回归CART
  • zemax色差与消色差
  • 成绩定级脚本(Python)
  • 骨传导耳机的危害有哪些?会损害听力吗?
  • Redis模块二:缓存分类 + Redis模块三:常见缓存(应用)
  • Revit SDK 内容摘要: 8.0 -8.1
  • 列表和字典练习
  • iwebsec靶场 文件包含漏洞通关笔记2-文件包含绕过(截断法)
  • 【基于Cocos Creator实现的赛车游戏】9.实现汽车节点的控制逻辑
  • 蓝蓝设计为教育行业提供软件UI交互设计服务
  • Java从入门到精通-类和对象(二)
  • Python解析MDX词典数据并保存到Excel
  • 线性代数的本质(四)
  • FreeMarker详细介绍
  • 房地产小程序 | 小程序赋能,房地产业务数字化升级
  • Databend 开源周报第 110 期
  • 开源大模型ChatGLM2-6B 1. 租一台GPU服务器测试下
  • SQL10 用where过滤空值练习
  • JVM--Hotspot Architecture 详解
  • ThreadLocal功能实现
  • Linux编辑器-vim使用
  • 自助式数据分析平台:JVS智能BI功能介绍(二)数据集管理
  • 《5G技术引领教育信息化新革命》
  • cmake学习过程记录
  • Vue3、Vite使用 html2canvas 把Html生成canvas转成图片并保存,以及填坑记录
  • centos yum源配置(CentOS7 原生 yum 源修改为阿里 yum 源)