I am using PyDev for development and unit-testing of my Python application.
As for unit-testing, everything works great except the fact that no content is logged to the logging framework. The logger is not captured by the “Captured output” of PyDev.
I’m already forwarding everything logged to the standard output like this:
import sys
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
Nevertheless the “Captured output” does not display the stuff logged to loggers.
Here’s an example unittest-script: test.py
import sys
import unittest
import logging
logger = logging.getLogger()
logger.level = logging.DEBUG
logger.addHandler(logging.StreamHandler(sys.stdout))
class TestCase(unittest.TestCase):
def testSimpleMsg(self):
print("AA")
logging.getLogger().info("BB")
The console output is:
Finding files... done.
Importing test modules ... done.
testSimpleMsg (itf.lowlevel.tests.hl7.TestCase) ... AA
2011-09-19 16:48:00,755 - root - INFO - BB
BB
ok
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
But the CAPTURED OUTPUT for the test is:
======================== CAPTURED OUTPUT =========================
AA
Does anybody know how to capture everything that is logged to a logging.Logger during the execution of this test?
The issue is that the
unittestrunner replacessys.stdout/sys.stderrbefore the testing starts, and theStreamHandleris still writing to the originalsys.stdout.If you assign the ‘current’
sys.stdoutto the handler, it should work (see the code below).Although, a better approach would be adding/removing the handler during the test: