Skip to main content

Plugin configuring handlers for loggers from Python logging module.

Project description

Pypi Package Version Supported Python Versions Documentation Status Coverage Status Travis-CI Build Status AppVeyor-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.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')

have logging tests or libraries (including fixtures):

#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')

and expect output in terminal (if not captured):

$ 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

being able to change this output by cmdline option:

$ 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

and - the same - in filesystem:

$ 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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-logger-0.3.0.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

pytest_logger-0.3.0-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-logger-0.3.0.tar.gz.

File metadata

  • Download URL: pytest-logger-0.3.0.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pytest-logger-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e20040a430f2947fbab97601ee217fbb733a851f0aa237fdfa68a779079968e8
MD5 4bf1fb68e9c3e88365633e23afd4ee34
BLAKE2b-256 7eb3326182714d20fb14baf18859e9de184328f307c3a9806f21f4a459ce183c

See more details on using hashes here.

File details

Details for the file pytest_logger-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_logger-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ee8dde3868c2aaf5f0a9e946f1d0cb3c6062938dc69a7c642274fb5398b64e9e
MD5 1bd58cbeddd262f5233cd3f8f2be4da9
BLAKE2b-256 a1e27b64d1eaa01115011f6d90f6ea0defc26e87a10c74bf905e9f35377c5822

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page