Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py.test logging control

Tags:

python

pytest

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

like image 624
jjh Avatar asked Aug 07 '12 22:08

jjh


People also ask

How do I use Pytest logging?

By setting the log_cli configuration option to true , pytest will output logging records as they are emitted directly into the console. You can specify the logging level for which log records with equal or higher level are printed to the console by passing --log-cli-level .

Does Pytest disable logging?

Pytest does not support this by default, but you can add a custom option to your conftest.py to turn off specific loggers.

What are the five levels of logging in Python?

In Python, the built-in logging module can be used to log events. Log messages can have 5 levels - DEBUG, INGO, WARNING, ERROR and CRITICAL. They can also include traceback information for exceptions. Logs can be especially useful in case of errors to help identify their cause.

How do I enable logging in Python?

You can configure logging as shown above using the module and class functions or by creating a config file or a dictionary and loading it using fileConfig() or dictConfig() respectively. These are useful in case you want to change your logging configuration in a running application.


4 Answers

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.

like image 133
hpk42 Avatar answered Oct 09 '22 17:10

hpk42


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)
like image 40
Aron Curzon Avatar answered Oct 09 '22 17:10

Aron Curzon


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.

like image 8
danodonovan Avatar answered Oct 09 '22 16:10

danodonovan


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.

like image 6
Alaya Avatar answered Oct 09 '22 17:10

Alaya