python unittest howto

后端 未结 3 1957
栀梦
栀梦 2021-01-14 10:38

I`d like to know how I could unit-test the following module.

def download_distribution(url, tempdir):
    \"\"\" Method which downloads the distribution from         


        
3条回答
  •  囚心锁ツ
    2021-01-14 11:29

    To mock urllopen you can pre fetch some examples that you can then use in your unittests. Here's an example to get you started:

    def urlopen(url):
        urlclean = url[:url.find('?')] # ignore GET parameters
        files = {
            'http://example.com/foo.xml': 'foo.xml',
            'http://example.com/bar.xml': 'bar.xml',
        }
        return file(files[urlclean])
    yourmodule.urllib.urlopen = urlopen
    

提交回复
热议问题