How can I see normal print output created during pytest run?
Activate immediate output during pytest by adding -s
. This disables output capturing, allowing print()
statements to be immediately visible.
Command:
Usage in Test:
Digging Deeper: Additional pytest output options
Outside the exemplary -s
switch, pytest offers you a bunch of knives to tailor your output viewing experience:
pytest -rP
to show input from tests that passed like a breezepytest -rx
for the ones that hit the wall of failurepytest --capture=tee-sys
, for brilliant fellows with pytest v5.4.0 and onwards: logs stdout/stderr and keeps it in the console.
Remember to be skeptical about plugins, such as pytest-runner. It might feel tempting, but sticking to pytest's native features ensures stability.
Troubleshooting: When output capture doesn't behave
Issues with capturing? Check your imports and pytest environment.
Order-impaired imports breed runtime issues: Import pytest
before _pytest
. Monkey-patching PyTest.run_tests()
? Be extra careful as stdout could be deceptive. Consider teeing stdout and stderr together for a panoramic view of the output.
A smart cookie would regularly check the pytest changelog and documentation to dodge quirks and bugs. Foresight saves the day!
Live on Stage: Output in PyCharm
PyCharm fans can rejoice with -s
, as it's a wit to in-IDE debugging. It brings life to stdout during test execution. It's as simple as attaching -s
in PyCharm's Run/Debug Configurations. Voila! Prints galore in the output console.
Experimenting: Exploring other capture methods
You can tap into the magic of the -r
flag. It gives you a summary based on the results:
-ra
: Unsilence all prints: revel in the details-rf
: It's a no-brainer: eyes on the failed tests.
Keep the testing pot boiling: Try custom fixtures that reel in capsys
or capfd
for more control. They let the print capture thrive even after the test. It's like an instant replay in slow motion!
Formidable Pro-Tips: Playing for Keeps
Avoid pitfalls by taking care of how you import and structure your test cases.
Avoid getting tangled in convoluted setups. Pick incremental testing: the subtle art of locating where output wavers.
The first line of defense always is the pytest documentation. Go back to it whenever your test scripts get untidy.
Was this article helpful?