How to unit test a downloading function?

python, unit-testing

Solution

If all you want to test is that the function calls urlretrieve on all elements in file_list you can modify the function to take the retrieve-function as a parameter:

def downloder(urlretrieve):
  file_list=['fileone.htm','filetwo.htm','filethree.htm']
  for f in file_list:
    (filename,headers) = urlretrieve(f,'c:\\temp\\'+f)

And then in your unit test you can create a custom function and check that is called the correct amount of times and with the correct parameters.

calls = []
def retrieve(url, local) :
  calls.append([url,local])

assert(len(calls) == 3)
assert(calls[0][0] == 'fileone.html')
assert(calls[0][2] == 'c:\\temp\\fileone.html')
...

You can use the library Mock to simplify the part of creating your own retrieve function for the unit test.

Problem

I have a python function that downloads a few files. e.g. ``` def downloader(): file_list=['fileone.htm','filetwo.htm','filethree.htm'] for f in file_list: (filename,headers) = urllib.urlretrieve(f,'c:\\temp\\'+f) ``` What is the correct way to unit test the function? Whether or not it works is dependent on how the urlretrieve function behaves, which is dependent on external factors.

Original source