Mocking two functions with patch for a unit test

后端 未结 3 433
陌清茗
陌清茗 2020-12-13 03:16

I have a function I want to unit test contains calls two other functions. I am unsure how can I mock both functions at the same time properly using patch. I have provided an

3条回答
  •  猫巷女王i
    2020-12-13 03:45

    In addition to the response @Matti John you can also use patch inside function test_write_out:

    from mock import MagicMock, patch
    
    def test_write_out():
        path = '~/collection'
        with patch('__builtin__.open') as mock_open, \
                patch('cPickle.dump') as mock_pickle:
    
            f = mock_open.return_value
            ...
    

提交回复
热议问题