I\'m writing tests for a function like next one:
def foo():
print \'hello world!\'
So when I want to test this function the code will b
A lot of these answers failed for me because you can't from StringIO import StringIO
in Python 3. Here's a minimum working snippet based on @naxa's comment and the Python Cookbook.
from io import StringIO
from unittest.mock import patch
with patch('sys.stdout', new=StringIO()) as fakeOutput:
print('hello world')
self.assertEqual(fakeOutput.getvalue().strip(), 'hello world')