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?


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.


commented: Nice discovery. I didn't know that ! +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.