When we implemented the test suite for the TODO list application, we had to check that the output provided by the application was the expected one. That meant that we provided a fake implementation of the standard output, which allowed us to see what the application was going to write.
Suppose you have a very simple app that prints something when started:
def myapp():
print("MyApp Started")
If we wanted to test that the app actually prints what we expect when started, we could use the capsys fixture to access the capture output from sys.stdout and sys.stderr of our application:
def test_capsys(capsys):
myapp()
out, err = capsys.readouterr()
assert out == "MyApp Started\n"
The test_capsys test just starts the application (running myapp), then through capsys.readouterr() it retrieves the content of sys.stdout and sys.stderr snapshotted at that moment.
Once the standard output content is available, it can be compared to the expected...