Using pytest, sometimes does not capture stderr
David
bouncingcats at gmail.com
Mon Apr 5 01:05:59 EDT 2021
On Mon, 5 Apr 2021 at 14:26, Cameron Simpson <cs at cskk.id.au> wrote:
> On 05Apr2021 13:56, David <bouncingcats at gmail.com> wrote:
> >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.
> Yes. Unsure what's going on there. It could be timing. Suppose this
> happens:
> - pytest pushes a capturing stderr onto sys.stderr
> - pytest loads your module, which imports module_1 and module_2
> - the test runner pushes a separate stderr capturer for the test?
> - module_1 finds the per-test sys.stderr value
> - module_2 finds pytest's outermost capturer (present when it was
> imported), and doesn't look up sys.stderr at test time, instead using
> the outer capturer
Ok. I do understand that my function parameter defaults are defined
when the function is defined, so something like this could happen.
> >2) My actual code that I would like to test does look like module_2.
> > Is there any way to test it with pytest?
> I'd be inclined to give msg() an optional file= parameter:
Understood.
> If you truly need to test msg() _without_ the file= parameter, you could
> monkey patch module_2:
I tried this, it works too.
Thanks so much for your guidance! It's great to be steered through
puzzlement by experts. I hope you're having a good day, you made
mine better :)
More information about the Python-list
mailing list