On 05/04/2021 06:25, Cameron Simpson wrote:

If you truly need to test msg() _without_ the file= parameter, you could
monkey patch module_2:

     old_MSG_DESTINATION = module_2.MSG_DESTINATION
     module_2.MSG_DESTINATION = sys.stderr
     # now the module_2 module has an updated reference for sys.stderr
     ...
     msg("a", "message")
     ...
     module_2.MSG_DESTINATION = old_MSG_DESTINATION
     # normality restored

I was about to write "use contextlib.redirect_sterr()", and noted my
error just before hitting send. There is a tool in the stdlib that might
work though:

from unittest import mock

with mock.patch("module_2.MSG_DESTINATION", sys.stderr):
    msg("a", "message")
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to