为来自python日志模块的日志记录器配置处理程序的插件。
pytest-logger的Python项目详细描述
pytest logger是一个pytest插件,为python日志模块中的日志记录器配置处理程序。
您可以通过pip从PyPI:
安装pytest记录器$ [sudo] pip install pytest-logger
插件根据每个记录器将日志放到:
- 标准输出,
- pytest的tmpdir_factorysession目录下的日志特定目录中的文件。
您可以使用钩子设置插件:
#conftest.py import os def pytest_logger_config(logger_config): logger_config.add_loggers(['foo', 'bar', 'baz'], stdout_level='info') logger_config.set_log_option_default('foo,bar') def pytest_logger_logdirlink(config): return os.path.join(os.path.dirname(__file__), 'mylogs')
进行日志记录测试或库(包括设备):
#test_something.py import pytest import logging foo = logging.getLogger('foo') bar = logging.getLogger('bar') baz = logging.getLogger('baz') @pytest.yield_fixture(scope='session') def session_thing(): foo.debug('constructing session thing') yield foo.debug('destroying session thing') @pytest.yield_fixture def testcase_thing(): foo.debug('constructing testcase thing') yield foo.debug('destroying testcase thing') def test_one(session_thing, testcase_thing): foo.info('one executes') bar.warning('this test does nothing aside from logging') baz.info('extra log, rarely read') def test_two(session_thing, testcase_thing): foo.info('two executes') bar.warning('neither does this') baz.info('extra log, not enabled by default')
并期望终端输出(如果未捕获):
$ py.test -s -v (...) test_something.py::test_one 00:00.002 inf foo: one executes 00:00.002 wrn bar: this test does nothing aside from logging PASSED test_something.py::test_two 00:00.000 inf foo: two executes 00:00.000 wrn bar: neither does this PASSED
能够通过命令行选项更改此输出:
$ pytest -s -v --log foo.debug,baz (...) test_something.py::test_one 00:00.002 dbg foo: constructing session thing 00:00.002 dbg foo: constructing testcase thing 00:00.002 inf foo: one executes 00:00.003 inf baz: extra log, rarely read PASSED test_something.py::test_two 00:00.000 dbg foo: constructing testcase thing 00:00.000 inf foo: two executes 00:00.001 inf baz: extra log, not enabled by default PASSED
和-相同-在文件系统中:
$ file mylogs mylogs: symbolic link to `/tmp/pytest-of-aurzenligl/pytest-48/logs' $ tree mylogs mylogs `-- test_something.py |-- test_one | |-- bar | |-- baz | `-- foo `-- test_two |-- bar |-- baz `-- foo
Pytest Logger根据MIT许可证的条款发布,是免费的开源软件。