I would like to put some logging statements within test function to examine some state variables.
I have the following code snippet:
import pytest,os
import logging
logging.basicConfig(level=logging.DEBUG)
mylogger = logging.getLogger()
#############################################################################
def setup_module(module):
''' Setup for the entire module '''
mylogger.info('Inside Setup')
# Do the actual setup stuff here
pass
def setup_function(func):
''' Setup for test functions '''
if func == test_one:
mylogger.info(' Hurray !!')
def test_one():
''' Test One '''
mylogger.info('Inside Test 1')
#assert 0 == 1
pass
def test_two():
''' Test Two '''
mylogger.info('Inside Test 2')
pass
if __name__ == '__main__':
mylogger.info(' About to start the tests ')
pytest.main(args=[os.path.abspath(__file__)])
mylogger.info(' Done executing the tests ')
I get the following output:
[bmaryada-mbp:/Users/bmaryada/dev/platform/main/proto/tests/tpch $]python minitest.py
INFO:root: About to start the tests
======================================================== test session starts =========================================================
platform darwin -- Python 2.6.2 -- pytest-2.0.0
collected 2 items
minitest.py ..
====================================================== 2 passed in 0.01 seconds ======================================================
INFO:root: Done executing the tests
Notice that only the logging messages from the '__name__ == __main__'
block get transmitted to the console.
Is there a way to force pytest to emit logging to console from test methods as well?
Since version 3.3,
pytest
supports live logging, meaning that all the log records emitted in tests will be printed to the terminal immediately. The feature is documented under Live Logs section. Live logging is disabled by default; to enable it, setlog_cli = 1
in thepytest.ini
config. Live logging supports emitting to terminal and file; the relevant options allow records customizing:terminal:
log_cli_level
log_cli_format
log_cli_date_format
file:
log_file
log_file_level
log_file_format
log_file_date_format
Note:
As pointed out by Kévin Barré in this comment, overriding ini options from command line can be done via thelog_cli
flag can't be passed from command line and must be set inpytest.ini
(orsetup.cfg
). All the other options can be both passed from command line or set in the config file.-o/--override
option. So instead of declaringlog_cli
inpytest.ini
/setup.cfg
, you can simply call:Examples
Simple test file used for demonstrating:
As you can see, no extra configuration needed;
pytest
will setup the logger automatically, based on options specified inpytest.ini
or passed from command line.Live logging to terminal,
INFO
level, fancy outputConfiguration in
pytest.ini
:Running the test:
Live logging to terminal and file, only message &
CRITICAL
level in terminal, fancy output inpytest.log
fileConfiguration in
pytest.ini
:Test run:
Works for me, here's the output I get: [snip -> example was incorrect]
Edit: It seems that you have to pass the
-s
option to py.test so it won't capture stdout. Here (py.test not installed), it was enough to usepython pytest.py -s pyt.py
.For your code, all you need is to pass
-s
inargs
tomain
:See the py.test documentation on capturing output.