0

When I call a Python unit test I can see all the stdout directly e.g. how remote APIs are called etc., if I use the -s switch.

python3 -m pytest -s some_test.py

However if I run a script with same code which I prototyped in the test script, the stdout comes only at the end.

python3 script.py

How to make script.py to print to stdout as it comes from pytest?

Open Food Broker
  • 1,095
  • 1
  • 8
  • 31
  • You can also do `pytest -s some_test.py` – Devesh Kumar Singh Jun 19 '19 at 12:26
  • This sounds like what you've got is *buffering* -- if your stdout is going to a file or pipeline, not a TTY, then it's collected into large chunks and flushed only when they're large enough, to reduce I/O overhead (whereas when it's going straight to a TTY, the default is to flush immediately, since it's assumed that a user is watching, so the overhead of frequent writes is worth paying). BTW, stderr, not stdout, is supposed to be used for diagnostic logs -- and it's unbuffered by default, even when directed to a non-TTY sink. – Charles Duffy Jun 19 '19 at 12:30
  • ...that said, `python3 script.py` itself isn't enough to know that buffering is happening or that it's not, because it doesn't show how your stdout is directed. (OTOH, that's pretty much the canonical way to get output to flush only at end-of-execution without meaning to). – Charles Duffy Jun 19 '19 at 12:32

0 Answers0