Can't catch mocked exception because it doesn't inherit BaseException

后端 未结 6 1650
无人共我
无人共我 2020-12-09 02:35

I\'m working on a project that involves connecting to a remote server, waiting for a response, and then performing actions based on that response. We catch a couple of diff

6条回答
  •  执念已碎
    2020-12-09 02:41

    Use patch.object to partially mock a class.

    My use case:

    import unittest
    from unittest import mock
    import requests
    
    def test_my_function(self):
        response = mock.MagicMock()
        response.raise_for_status.side_effect = requests.HTTPError
    
        with mock.patch.object(requests, 'get', return_value=response):
            my_function()
    

提交回复
热议问题