jjh
jjh

Reputation: 554

py.test logging control

We have recently switched to py.test for python testing (which is fantastic btw). However, I'm trying to figure out how to control the log output (i.e. the built-in python logging module). We have pytest-capturelog installed and this works as expected and when we want to see logs we can pass --nologcapture option.

However, how do you control the logging level (e.g. info, debug etc.) and also filter the logging (if you're only interested in a specific module). Is there existing plugins for py.test to achieve this or do we need to roll our own?

Thanks, Jonny

Upvotes: 31

Views: 25568

Answers (5)

Alaya
Alaya

Reputation: 282

Pytest now has native support for logging control via the caplog fixture; no need for plugins.

You can specify the logging level for a particular logger or by default for the root logger:

import pytest

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

Pytest also captures log output in caplog.records so you can assert logged levels and messages. For further information see the official documentation here and here.

Upvotes: 7

aurzenligl
aurzenligl

Reputation: 181

A bit of an even later contribution: you can try pytest-logger. Novelty of this plugin is logging to filesystem: pytest provides nodeid for each test item, which can be used to organize test session logs directory (with help of pytest tmpdir facility and it's testcase begin/end hooks).

You can configure multiple handlers (with levels) for terminal and filesystem separately and provide own cmdline options for filtering loggers/levels to make it work for your specific test environment - e.g. by default you can log all to filesystem and small fraction to terminal, which can be changed on per-session basis with --log option if needed. Plugin does nothing by default, if user defines no hooks.

Upvotes: 1

danodonovan
danodonovan

Reputation: 20373

A bit of a late contribution, but I can recommend pytest-logging for a simple drop-in logging capture solution. After pip install pytest-logging you can control the verbosity of the your logs (displayed on screen) with

$ py.test -s -v tests/your_test.py
$ py.test -s -vv tests/your_test.py
$ py.test -s -vvvv tests/your_test.py

etc... NB - the -s flag is important, without it py.test will filter out all the sys.stderr information.

Upvotes: 8

Aron Curzon
Aron Curzon

Reputation: 2644

As Holger said you can use pytest-capturelog:

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

If you don't want to use pytest-capturelog you can use a stdout StreamHandler in your logging config so pytest will capture the log output. Here is an example basicConfig

logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)

Upvotes: 10

hpk42
hpk42

Reputation: 23631

Installing and using the pytest-capturelog plugin could satisfy most of your pytest/logging needs. If something is missing you should be able to implement it relatively easily.

Upvotes: 10

Related Questions