How can I combine stdlib logging with py.test

I use py.test to test some of my modules that contain quite a few stdlib entries. Of course, I would like the log to be logged on stdout, which was captured by py.test, so that I get all relevant log messages if the test fails.

The problem is that the logging module completes the attempt to write messages to the 'stdout' object provided by py.test after this object has been dropped by py.test. That is, I get:

Traceback (most recent call last):
  File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
    h.flush()
  File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
    self.stream.flush()
ValueError: I/O operation on closed file

If I disable capture with -s, I have no problem, but of course, this makes the test output unreadable with irrelevant logging.

Can someone tell me the correct way to integrate stdlib entry with py.test?

(I tried to look at this one , where it looks like it should work without problems, so that didn't help me)

+5
source share
3 answers

Interaction for logging / capture is the best work with the upcoming version 2.0.1, which you can install already as a development snapshot:

pip install -i http://pypi.testrun.org pytest 

After entering "py.test --version" you should get at least "2.0.1.dev9". And the problem / error you posted should now go away.

: "" , , sys.stderr , atexit. py.test sys.stdout ( -, ). , py.test , , , atexit-.

[pytest-capturelog] [1], .

[1] http://pypi.python.org/pypi/pytest-capturelog/0.7

+5

, , py.test, logging.raiseExceptions False - .

, , . ( , , )

logging.basicConfig() . , , , .

+3

, , -s py.test, stdout.

-2

All Articles