I have been using doctest for a while and just recently started using the logging module. Each works fine separately, but they don't seem to play nice together. It looks like doctest hijacks sys.stdout to do its thing. My logger prints to the screen if not in batch and to a file if it is, so for the doctest I am running interactively, the logger output goes to logging.StreamHandler(). Other parts of the code still write to sys.stdout.

What happens is that doctest appears to continue to work on sys.stdout, but ignores the logging output. The logging output prints to the screen even when the doctest runs correctly. I really would like to have these two output streams merged just like they are when I run the script directly, and have doctest compare the merged output. My guess is that there is probably a way to do this?

Similarly, if I sent everything to the logger, what would I have to do to get doctest to work on the logging output stream?

Thanks,
John

And the answer is:

Change the logging handler from logging.StreamHandler() to logging.StreamHandler(sys.stdout)

Apparently logging.StreamHandler defaults to sys.stderr.

Simple answer, but difficult to figure out.

Anyway, things work for me now.

jrt

Comments
Nice discovery. I didn't know that !
This question has already been answered. Start a new discussion instead.