What if you could test printed messages without ever looking at the screen?
Why capsys for capturing output in PyTest? - Purpose & Use Cases
Imagine you run a program that prints messages to the screen, and you want to check if those messages are correct. You try to watch the screen carefully every time you run it to see if the output is right.
Watching the screen manually is slow and easy to miss mistakes. If the program prints a lot or runs many times, you get tired or distracted. You might forget what you saw or mix up outputs from different runs.
Using capsys in pytest lets you catch all printed messages automatically during tests. You can then check these messages in your code without looking at the screen. This makes testing fast, clear, and repeatable.
print('Hello') # Manually check the screen output
def test_output(capsys): print('Hello') captured = capsys.readouterr() assert captured.out == 'Hello\n'
It enables automatic checking of printed messages, making tests reliable and saving you from watching the screen every time.
When building a calculator app that prints results, you can use capsys to verify the printed answers are correct without reading the screen manually.
Manual output checking is slow and error-prone.
capsys captures printed output automatically in tests.
This makes output verification fast, clear, and repeatable.