Mock Python's built in print function

前端 未结 11 1953
醉话见心
醉话见心 2020-12-06 09:10

I\'ve tried

from mock import Mock
import __builtin__

__builtin__.print = Mock()

But that raises a syntax error. I\'ve also tried patching

11条回答
  •  离开以前
    2020-12-06 09:26

    My version.

    In the tested program(ex: pp.py):

    from __future__ import print_function
    
    def my_func():
        print('hello')
    

    In the test program:

    def test_print(self):
        from pp import my_func
        from mock import call
        with mock.patch('__builtin__.print') as mock_print:
           my_func()
           mock_print.assert_has_calls(
                [
                    call('hello')
                ]
            )
    

提交回复
热议问题