Pytest plugin configuring handlers for loggers from Python logging module.

aurzenligl aurzenligl Last update: Jun 24, 2022
Pypi Package VersionSupported Python VersionsDocumentation StatusCoverage StatusTravis-CI Build StatusAppVeyor-CI Build Status

Pytest-logger is a pytest plugin configuring handlers for loggers from Python logging module.

You can install pytest-logger via pip from PyPI:

$ [sudo] pip install pytest-logger

Plugin puts logs on per-logger basis to:

  • standard output,
  • files within log-specific directory under pytest's tmpdir_factory session directory.

You can setup plugin using hook:

#conftest.pyimport osdef 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')

have logging tests or libraries (including fixtures):

#test_something.pyimport pytestimport loggingfoo = 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_fixturedef 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')

and expect output in terminal (if not captured):

$ py.test -s -v(...)test_something.py::test_one00:00.002 inf foo: one executes00:00.002 wrn bar: this test does nothing aside from loggingPASSEDtest_something.py::test_two00:00.000 inf foo: two executes00:00.000 wrn bar: neither does thisPASSED

being able to change this output by cmdline option:

$ pytest -s -v --log foo.debug,baz(...)test_something.py::test_one00:00.002 dbg foo: constructing session thing00:00.002 dbg foo: constructing testcase thing00:00.002 inf foo: one executes00:00.003 inf baz: extra log, rarely readPASSEDtest_something.py::test_two00:00.000 dbg foo: constructing testcase thing00:00.000 inf foo: two executes00:00.001 inf baz: extra log, not enabled by defaultPASSED

and - the same - in filesystem:

$ file mylogsmylogs: symbolic link to `/tmp/pytest-of-aurzenligl/pytest-48/logs'$ tree mylogsmylogs`-- test_something.py    |-- test_one    |   |-- bar    |   |-- baz    |   `-- foo    `-- test_two        |-- bar        |-- baz        `-- foo

Distributed under the terms of the MIT license, pytest-logger is free and open source software.

Tags:

PRAGMA foreign_keys = off; BEGIN TRANSACTION; COMMIT TRANSACTION; PRAGMA foreign_keys = on;

Subscribe to our newsletter