You are testing a function in unit test using python. The function emits some text output
sys.stdout, but you want to suppress the output during unit testing (simply because
the output is irrelevant during testing).
patch() function, it’s pretty simple to mock out
sys.stdout for just a single test, and put it back again, without messy temporary variables
or leaking mocked-out state between test cases. For example:
mock is a library for testing in Python.
It allows you to replace parts of your system under test with mock objects and makes
assertions about how they have been used. In the above example, it is used to replace
std.stdout temporarily ONLY within the
block, and restore it back afterwards.
And of course, the result will be that the text ‘some message texts to stdout …’ simply disappear while running unit test.
The above idea is inspired by the following web resources: