pytest log配置

发现用print在console里面打不出来,所以查了一下关于pytest的log配置,记录

首先需要在根目录新建 pytest.ini

如果你只需要使用print打印日志的话,就只需要这样写

[pytest]
addopts = -s
#addopts 是一个配置项,用于指定传递给 pytest 的额外命令行选项。
#-s 是 pytest 的一个命令行选项,表示禁用输出捕获,允许测试中的 print 语句将输出直接打印到控制台。
#因此,这个配置告诉 pytest 在运行时使用 -s 选项,使得测试中的输出能够直接显示在终端上,而不是被 #pytest 捕获并隐藏。这在调试和查看详细信息时很有用。 

就可以在pytest中在console中看到print打印的东西呢。

但是如果需要log配置的话。

[pytest]
log_cli = true
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format = %Y-%m-%d %H:%M:%S

然后在test中这样就可以使用了 


import pytest
import logging

log=logging.getLogger(__name__)

@pytest.fixture
def xxxx():
    ...
    yield xxxx


def test_xxx(xxxx):
    log.error("Log Something")
    ...
    
      

如果要和flask的log全局配置结合使用,请参考我的另一篇blog:
Flask 3.x log全域配置(包含pytest)-CSDN博客

相关推荐

  1. Git<span style='color:red;'>配置</span>

    Git配置

    2024-01-20 06:14:01      62 阅读
  2. Redisson配置

    2024-01-20 06:14:01       59 阅读
  3. nginx配置

    2024-01-20 06:14:01       61 阅读
  4. VLAN配置

    2024-01-20 06:14:01       55 阅读
  5. XMLParser配置

    2024-01-20 06:14:01       57 阅读
  6. Redis 配置

    2024-01-20 06:14:01       47 阅读
  7. NAT<span style='color:red;'>配置</span>

    NAT配置

    2024-01-20 06:14:01      61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 06:14:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 06:14:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 06:14:01       82 阅读
  4. Python语言-面向对象

    2024-01-20 06:14:01       91 阅读

热门阅读

  1. JVM实战(26)——SystemGC

    2024-01-20 06:14:01       59 阅读
  2. PHP 把秒转换为多少天/小时/分钟

    2024-01-20 06:14:01       56 阅读
  3. Tomcat Notes: Common Issues Of Tomcat

    2024-01-20 06:14:01       34 阅读
  4. React底层原理分析(简单大白话版本)

    2024-01-20 06:14:01       50 阅读
  5. Linux编译器--git、yum、gdb的使用

    2024-01-20 06:14:01       51 阅读
  6. 【Vue】列表渲染

    2024-01-20 06:14:01       53 阅读
  7. 如何在指定的conda虚拟环境中启动Juptyter

    2024-01-20 06:14:01       54 阅读
  8. react中refs的作用是什么?有几种用法?

    2024-01-20 06:14:01       46 阅读