Issue
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?
Solution
print
is a keyword in python 2.x, using it as attribute raises a SyntaxError. You can avoid that by using from __future__ import print_function
in the beginning of the file.
Note: you can't simply use setattr
, because the print function you modified doesn't get invoked unless the print
statement is disabled.
Edit: you also need to from __future__ import print_function
in every file you want your modified print
function to be used, or it will be masked by the print
statement.
Answered By - quantum
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.