Using pytest, sometimes does not capture stderr
David
bouncingcats at gmail.com
Sun Apr 4 23:56:27 EDT 2021
On Mon, 5 Apr 2021 at 13:44, Cameron Simpson <cs at cskk.id.au> wrote:
> On 05Apr2021 13:28, David <bouncingcats at gmail.com> wrote:
> >Can anyone explain why the module_2.py test fails?
> >Is it because stderr during module import is not the same as during test?
> >Is it something to do with mutable defaults?
> >How to investigate this?
> >And how can I get the test to pass without changing module_2?
> The code in module_2.py runs at different times.
> When it is imported, sys.stderr is the OS-provided stderr. That
> reference is kept in MSG_DESTINATION.
> Then your test code runs, and changes sys.stderr. It then runs msg(),
> which writes to the _original_ sys.stderr as preserved by
> MSG_DESTINATION. Thus not captured.
> By contrast, module_1.py looks up sys.stderr inside msg(), and finds the
> new one the code harness put at sys.stderr. So it writes to the thing
> that captures stuff.
Hi Cameron,
Thanks for confirming my suspicions so quickly. What you wrote
makes sense, but there are two points that still puzzle me.
1) The final line of the pytest failure output seems to shows that
pytest did capture (or is at least aware of) the stderr message
from module_2.
2) My actual code that I would like to test does look like module_2.
Is there any way to test it with pytest?
Thanks.
More information about the Python-list
mailing list