Skip to main content

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 log ---------------------------
test_pytest_catchlog.py    26 INFO     text going to logger
------------------------- Captured stdout -------------------------
text going to stdout
------------------------- Captured stderr -------------------------
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 log ---------------------------
2010-04-10 14:48:44 INFO text going to logger
------------------------- Captured stdout -------------------------
text going to stdout
------------------------- Captured stderr -------------------------
text going to stderr
==================== 2 failed in 0.02 seconds =====================

Further it is possible to disable capturing of logs completely with:

py.test --nocatchlog test_pytest_catchlog.py

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

------------------------- Captured stdout -------------------------
text going to stdout
------------------------- Captured stderr -------------------------
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.setLevel(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.setLevel(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.atLevel(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.atLevel(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'),
    ]

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.0.zip (10.5 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.0-py2.py3-none-any.whl (6.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

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

File hashes

Hashes for pytest-catchlog-1.0.zip
Algorithm Hash digest
SHA256 215c8f3a247196bd6fcf59f95f978c3b7deef28fbcdb4014d254bc81e8398483
MD5 a1f16aac5420c5e6724371d1aa211dec
BLAKE2b-256 0902490f51b6fe18ccd23e105f8fbfea2df330e6442e1184a6796759d87504e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pytest_catchlog-1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4b3b9d1819cc00b836b26caf559eaf49de6ec0325ba7c84745d96924f09c3a28
MD5 3af2e8fb2731909036175fa47df8af21
BLAKE2b-256 8db88499bfa70e18b2f770a3050eda55d362887da5734b77fc3ca8fd78615e2a

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1

2 files

This release

1.0 This release

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