Skip to main content

py.test plugin to capture logbook log messages

Project description

Capture and show logbook logging output during test runs.

This pytest plugin provides will capture log messages emitted by logbook and display them in their own section in the test report if the test fails. Additionally it provides a fixture which provides easy access to the log records emitted during the test which allows making assertions on emitted log message. The plugin also sets up logging redirection from the stdlib to go via logbook so any libraries which use stdlib logging will also be captured.

Installation

Install using pip:

pip install pytest-logbook

Log Capturing

Log capturing happens automatically once installed, including stdlib logging redirection:

def test_foo():
    logbook.info('hello')
    logging.info('world')
    assert 0
$ py.test test_foo.py
=========================== test session starts ============================
platform linux -- Python 3.4.3, pytest-2.8.2, py-1.4.30, pluggy-0.3.1
rootdir: /home/flub/Abilisoft/repos/pytest-logbook, inifile:
plugins: logbook-0.1.0
collected 1 items

test_foo.py F

================================= FAILURES =================================
_________________________________ test_foo _________________________________

    def test_foo():
        logbook.info('hello')
        logging.info('world')
>       assert 0
E       assert 0

test_foo.py:7: AssertionError
--------------------------- Logbook call records ---------------------------
[INFO] Generic: hello
[INFO] root: world
========================= 1 failed in 0.01 seconds =========================

Logs messages emitted during test setup and teardown are also captured individually. If setup fails you will see them in a “Logbook setup records” section and if teardown fails you will see the teardown log messages in a “Logbook teardown records” section.

Fixtures

Several fixtures are provided to access the captured log records.

loghandler

Access the logbook.TestHandler instance active during the test run. You can use it to inspect the emitted log records, see the full documentation for details on how to use this object. Only log records emitted during the actual test are recorded here, records emitted during test setup or teardown are recorded in the other fixtures.

loghandler_setup

Access the logbook.TestHandler instance active during the test setup.

loghandler_teardown

Access the logbook.TestHandler instance active during the test teardown.

Configuration

The plugin can be configured using options in the py.test configuration file in the standard [pytest] section.

logbook_stdlib

This configures whether the stdlib logging is redirected to logbook. Set it to either true (default) or false.

Changelog

1.2.0

  • Refactor internal workings to improve behaviour during keyboard interrupt and internal py.test errors. As a minor regression and to keep things simple log records are now only captured during test setup, call or teardown phases and are not captured during conftest loading or test collection. This is not an inherent limitation, only avoiding unneeded features.

  • Fix using fixture in a test method rather then just a test function.

1.1.0

  • Do not fail if the application corrupts the Logbook stack.

1.0.3

  • Require Logbook 0.11.3 to get the logging.compat module accessible.

1.0.2

  • Include LICENSE.

  • Add MANIFEST.in in ship LICENSE, tox.ini and test module.

  • Start keeping a changelog.

  • Fix dependency to require py.test >= 2.8 due to how hooks are implemented.

1.0.1

  • Do not build universal wheel. Only tested/supported on python3.

1.0.1

  • Initial release.

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-logbook-1.2.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

pytest_logbook-1.2.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pytest-logbook-1.2.0.tar.gz.

File metadata

File hashes

Hashes for pytest-logbook-1.2.0.tar.gz
Algorithm Hash digest
SHA256 9454453230989a3d5149689c3449445554808d429b651a3d89c4464054a4c2dc
MD5 f120e731ff643e2fbff37f197ba1c561
BLAKE2b-256 d01ad3d5c361d6c5030ea2b0165081cb43e95d9e1624065dc383fb1cf35f4792

See more details on using hashes here.

File details

Details for the file pytest_logbook-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_logbook-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e2e79cf188fdae3fc7c857a168a9f18a5d9cca6234f38795b3ff628d23e2be17
MD5 b7cda0c72256e94ce66f4a4009afc6d8
BLAKE2b-256 fc74585cf76ac05324380cd7c847a824736d40b90882825c4d735f37190b596f

See more details on using hashes here.

Supported by

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