Using pytest, sometimes does not capture stderr
Peter Otten
__peter__ at web.de
Mon Apr 5 07:14:09 EDT 2021
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")
More information about the Python-list
mailing list