I’ve tried
from mock import Mock
import __builtin__
__builtin__.print = Mock()
But that raises a syntax error. I’ve also tried patching it like so
@patch('__builtin__.print')
def test_something_that_performs_lots_of_prints(self, mock_print):
# assert stuff
Is there any way to do this?
printis a keyword in python 2.x, using it as attribute raises a SyntaxError. You can avoid that by usingfrom __future__ import print_functionin the beginning of the file.Note: you can’t simply use
setattr, because the print function you modified doesn’t get invoked unless theprintstatement is disabled.Edit: you also need to
from __future__ import print_functionin every file you want your modifiedprintfunction to be used, or it will be masked by theprintstatement.