How to test print statements?

后端 未结 2 2033
后悔当初
后悔当初 2021-02-18 13:12

You want to write unittest-cases for a function like that:

def test_me(a):
    for b in c:
        print do_something(a,b)

At firs

2条回答
  •  失恋的感觉
    2021-02-18 13:47

    In Python 3 it's easy to use unittest.mock on the builtin print function:

    from unittest.mock import patch, call
    
    @patch('builtins.print')
    def test_print(mocked_print):
        print('foo')
        print()
    
        assert mocked_print.mock_calls == [call('foo'), call()]
    

提交回复
热议问题