我想在测试函数中放入一些日志语句来检查一些状态变量。

我有以下代码片段:

import pytest,os
import logging

logging.basicConfig(level=logging.DEBUG)
mylogger = logging.getLogger()

#############################################################################

def setup_module(module):
    ''' Setup for the entire module '''
    mylogger.info('Inside Setup')
    # Do the actual setup stuff here
    pass

def setup_function(func):
    ''' Setup for test functions '''
    if func == test_one:
        mylogger.info(' Hurray !!')

def test_one():
    ''' Test One '''
    mylogger.info('Inside Test 1')
    #assert 0 == 1
    pass

def test_two():
    ''' Test Two '''
    mylogger.info('Inside Test 2')
    pass

if __name__ == '__main__':
    mylogger.info(' About to start the tests ')
    pytest.main(args=[os.path.abspath(__file__)])
    mylogger.info(' Done executing the tests ')

我得到以下输出:

[bmaryada-mbp:/Users/bmaryada/dev/platform/main/proto/tests/tpch $]python minitest.py
INFO:root: About to start the tests 
======================================================== test session starts =========================================================
platform darwin -- Python 2.6.2 -- pytest-2.0.0
collected 2 items 

minitest.py ..

====================================================== 2 passed in 0.01 seconds ======================================================
INFO:root: Done executing the tests 

注意,只有来自'__name__ == __main__'块的日志消息被传输到控制台。

是否有一种方法可以强制pytest也从测试方法向控制台发送日志?


当前回答

你可以阅读: 用于登录pytest的文档 下面是一个简单的例子,你可以从foo函数中运行并获取log。

#./test_main.py
from main import foo
import logging

def test_foo(caplog):
    caplog.set_level(logging.INFO)

    logging.getLogger().info('Log inside a test function!')

    assert foo(1, 2) == 5
    /* your test here*/
# ./main.py
import logging

def foo(a, b):
    logging.getLogger().info('a: ' + str(a))
    logging.getLogger().info('b: ' + str(b))
    return a + b

现在可以运行pytest并从函数中获取所需的日志信息。 如果没有任何错误,日志将被省略。

其他回答

如果您使用vscode,请使用以下配置,假设您已经安装 Python官方插件(ms-python.python)用于您的Python项目。

/ .vscode /设置。Json在你的proj

{
  ....
  "python.testing.pytestArgs": ["-s", "src"], //here before discover-path src
  "python.testing.unittestEnabled": false,
  "python.testing.nosetestsEnabled": false,
  "python.testing.pytestEnabled": true,
  ...
}

附注:一些插件可以在上面工作,包括但不限于:

Visual Studio代码Python测试资源管理器(littlefoxteam. vcode - Python - Test -adapter) Visual Studio代码测试资源管理器

使用pytest——log-cli-level=DEBUG可以很好地使用pytest(从6.2.2测试到7.1.1)

使用pytest——log-cli-level=DEBUG——capture=tee-sys也会打印标准输出。

你可以阅读: 用于登录pytest的文档 下面是一个简单的例子,你可以从foo函数中运行并获取log。

#./test_main.py
from main import foo
import logging

def test_foo(caplog):
    caplog.set_level(logging.INFO)

    logging.getLogger().info('Log inside a test function!')

    assert foo(1, 2) == 5
    /* your test here*/
# ./main.py
import logging

def foo(a, b):
    logging.getLogger().info('a: ' + str(a))
    logging.getLogger().info('b: ' + str(b))
    return a + b

现在可以运行pytest并从函数中获取所需的日志信息。 如果没有任何错误,日志将被省略。

如果你想用命令行过滤日志,你可以传递——log-cli-level (pytest——log-cli-level) 并且将显示您指定的级别及以上级别的日志

(例如,pytest——log-cli-level=INFO将显示INFO和以上日志(警告,错误,关键))

注意:default——log-cli-level如果你没有指定它是一个警告(https://docs.pytest.org/en/6.2.x/logging.html)

但是如果你不想每次都使用——log-cli-level使用pytest, 可以设置log-level 在你的pytest配置文件中(pytest.ini/tox.ini/setup.cfg)

e.g.

在pytest.ini(或我提到的其他配置文件)中放入log-level=INFO

运行pytest时,只能看到INFO和以上日志

要打开记录器输出,请从命令行使用send——capture=no标志。 ——capture=no将显示logger和print语句的所有输出。如果你想从记录器获取输出,而不是打印语句,请使用——capture=sys

pytest --capture=no tests/system/test_backoffice.py

以下是关于“捕获标准输出/标准derr输出”的更多信息

默认记录器输出级别为“警告” 更改日志输出级别使用——log-cli-level标志。

pytest --capture=no --log-cli-level=DEBUG tests/system/test_backoffice.py