How can one mock/stub python module like urllib

后端 未结 8 2079
情深已故
情深已故 2020-11-28 20:47

I need to test a function that needs to query a page on an external server using urllib.urlopen (it also uses urllib.urlencode). The server could be down, the page could cha

8条回答
  •  死守一世寂寞
    2020-11-28 21:20

    The simplest way is to change your function so that it doesn't necessarily use urllib.urlopen. Let's say this is your original function:

    def my_grabber(arg1, arg2, arg3):
        # .. do some stuff ..
        url = make_url_somehow()
        data = urllib.urlopen(url)
        # .. do something with data ..
        return answer
    

    Add an argument which is the function to use to open the URL. Then you can provide a mock function to do whatever you need:

    def my_grabber(arg1, arg2, arg3, urlopen=urllib.urlopen):
        # .. do some stuff ..
        url = make_url_somehow()
        data = urlopen(url)
        # .. do something with data ..
        return answer
    
    def test_my_grabber():
        my_grabber(arg1, arg2, arg3, urlopen=my_mock_open)
    

提交回复
热议问题