Skip to main content

pytest-catchlog

py.test plugin to catch log messages. This is a fork of pytest-capturelog.

Installation

The pytest-catchlog package may be installed with pip or easy_install:

pip install pytest-catchlog
easy_install pytest-catchlog

Usage

If the plugin is installed log messages are captured by default and for each failed test will be shown in the same manner as captured stdout and stderr.

Running without options:

py.test test_pytest_catchlog.py

Shows failed tests like so:

----------------------- Captured stdlog call ----------------------
test_pytest_catchlog.py    26 INFO     text going to logger
----------------------- Captured stdout call ----------------------
text going to stdout
----------------------- Captured stderr call ----------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

By default each captured log message shows the module, line number, log level and message. Showing the exact module and line number is useful for testing and debugging. If desired the log format and date format can be specified to anything that the logging module supports.

Running pytest specifying formatting options:

py.test --log-format="%(asctime)s %(levelname)s %(message)s" \
        --log-date-format="%Y-%m-%d %H:%M:%S" test_pytest_catchlog.py

Shows failed tests like so:

----------------------- Captured stdlog call ----------------------
2010-04-10 14:48:44 INFO text going to logger
----------------------- Captured stdout call ----------------------
text going to stdout
----------------------- Captured stderr call ----------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

These options can also be customized through a configuration file:

[pytest]
log_format = %(asctime)s %(levelname)s %(message)s
log_date_format = %Y-%m-%d %H:%M:%S

Although the same effect could be achieved through the addopts setting, using dedicated options should be preferred since the latter doesn’t force other developers to have pytest-catchlog installed (while at the same time, addopts approach would fail with ‘unrecognized arguments’ error). Command line arguments take precedence.

Further it is possible to disable reporting logs on failed tests completely with:

py.test --no-print-logs test_pytest_catchlog.py

Shows failed tests in the normal manner as no logs were captured:

----------------------- Captured stdout call ----------------------
text going to stdout
----------------------- Captured stderr call ----------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

Inside tests it is possible to change the log level for the captured log messages. This is supported by the caplog funcarg:

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

By default the level is set on the handler used to catch the log messages, however as a convenience it is also possible to set the log level of any logger:

def test_foo(caplog):
    caplog.set_level(logging.CRITICAL, logger='root.baz')
    pass

It is also possible to use a context manager to temporarily change the log level:

def test_bar(caplog):
    with caplog.at_level(logging.INFO):
        pass

Again, by default the level of the handler is affected but the level of any logger can be changed instead with:

def test_bar(caplog):
    with caplog.at_level(logging.CRITICAL, logger='root.baz'):
        pass

Lastly all the logs sent to the logger during the test run are made available on the funcarg in the form of both the LogRecord instances and the final log text. This is useful for when you want to assert on the contents of a message:

def test_baz(caplog):
    func_under_test()
    for record in caplog.records:
        assert record.levelname != 'CRITICAL'
    assert 'wally' not in caplog.text

For all the available attributes of the log records see the logging.LogRecord class.

You can also resort to record_tuples if all you want to do is to ensure, that certain messages have been logged under a given logger name with a given severity and message:

def test_foo(caplog):
    logging.getLogger().info('boo %s', 'arg')

    assert caplog.record_tuples == [
        ('root', logging.INFO, 'boo arg'),
    ]

Changelog

List of notable changes between pytest-catchlog releases.

Unreleased

Yet to be released.

1.2.1

Released on 2015-12-07.

  • [Bugfix] #18 - Allow caplog.records() to be modified. Thanks to Eldar Abusalimov for the PR and Marco Nenciarini for reporting the issue.

  • [Bugfix] #15 #17 - Restore Python 2.6 compatibility. (Thanks to Marco Nenciarini!)

Version 1.2

Released on 2015-11-08.

  • [Feature] #6 - Configure logging message and date format through ini file.

  • [Feature] #7 - Also catch logs from setup and teardown stages.

  • [Feature] #7 - Replace deprecated __multicall__ use to support future Py.test releases.

  • [Feature] #11 - reintroduce setLevel and atLevel to retain backward compatibility with pytest-capturelog. Also the members text, records and record_tuples of the caplog fixture can be used as properties now.

Special thanks for this release goes to Eldar Abusalimov. He provided all of the changed features.

Version 1.1

Released on 2015-06-07.

  • #2 - Explicitly state Python3 support and add configuration for running tests with tox on multiple Python versions. (Thanks to Jeremy Bowman!)

  • Add an option to silence logs completely on the terminal.

Version 1.0

Released on 2014-12-08.

  • Add record_tuples for comparing recorded log entries against expected log entries with their logger name, severity and formatted message.

Download files

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

Source Distribution

pytest-catchlog-1.2.1.zip (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_catchlog-1.2.1-py2.py3-none-any.whl (10.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pytest-catchlog-1.2.1.zip.

File metadata

  • Download URL: pytest-catchlog-1.2.1.zip
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pytest-catchlog-1.2.1.zip
Algorithm Hash digest
SHA256 9e9ecdeff56666f8a2fd1bee2f6ca3b1be30f1fcfb17fdf9bbd0e95d53feec83
MD5 74603999f2132a6afc4147a324ac93dc
BLAKE2b-256 0c06dfc4deb6657c92199c4c171f6aca05be4b939a17759e6634437da257a596

See more details on using hashes here.

File details

Details for the file pytest_catchlog-1.2.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_catchlog-1.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f7835e01ad773030de32fda5f76a8a11a46771ea885de01878a3e72510eb7b28
MD5 5a9ec4fd8f3711b1bafc2b6c62b1edc9
BLAKE2b-256 8d4c0f388264fe12b4b86fb3e7c275ac10dadfe697f7c4274c69ce819e0f806f

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.2

2 files

This release

1.2.1 This release

2 files

1.2.0

2 files

1.1

2 files

1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page