Skip to content Skip to sidebar Skip to footer

Pytest Logging Ignores Options In Pytest.ini

I have a test that I am running with: pytest --capture=no --verbose --rootdir=testing/ testing/tests/docker_test.py from /home/user/development/. The test checks if some container

Solution 1:

I guess what you are missing is log_cli = 1 (or true/yes/etc) in your pytest.ini. Besides that, with the config you're provided the log records are printed in the format you specified in log_cli_format. You can even reduce the pytest.ini to:

[pytest]log_cli = 1log_cli_level = INFO
log_cli_format = %(asctime)s %(levelname)s %(message)s
log_cli_date_format = %H:%M:%S

Also, the above config will take care of the root logger config in test session, so you shouldn't need to configure the logger in tests for live logging. Just call the logger in tests:

import logging

def test_spam():
    logger = logging.getLogger(__name__)
    logger.info('spam')
    logger.warning('eggs')
    logger.error('bacon')

This will print:

$ pytest
============================== test session starts ================================
platform linux -- Python 3.6.5, pytest-3.4.1, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64/usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /data/gentoo64/home/u0_a82/projects/stackoverflow/so-50677656, inifile: pytest.ini
plugins: mock-1.6.3, cov-2.5.1, flaky-3.4.0
collected 1 item

testing/tests/test_docker.py::test_logs
---------------------------------- live log call ----------------------------------
16:29:12 INFO spam
16:29:13 WARNING eggs
16:29:13 ERROR bacon
PASSED                                                                       [100%]
============================ 1 passed in 1.08 seconds =============================

For one I am wondering what asctime stands for

The logging docs are a bit laconic about it:

Human-readable time when the LogRecord was created. By default this is of the form ‘2003-07-08 16:49:45,896’ (the numbers after the comma are millisecond portion of the time).

However, asctime does not mean that the records will be always formatted using time.asctime - it's only the default datetime format used when you don't pass your own to the logging.Formatter (second argument in the formatter constructor).

Post a Comment for "Pytest Logging Ignores Options In Pytest.ini"