[英]How to unit test this code with pymox?
所以我已經安裝了pymox,我想測試一下這個方法:
class HttpStorage():
def download(self, input, output):
try:
file_to_download = urllib2.urlopen(input)
except URLError:
raise IOError("Opening input URL failed")
f = open(output, "wb")
f.write(file_to_download.read())
f.close()
return True
我正在閱讀pymox文檔,但無法弄清楚該怎么做。 您能幫我一些示例代碼嗎?
import unittest
import mox
class HttpStorageTest(mox.MoxTestBase):
def setUp(self):
self.httpstorage = HttpStorage()
def test_download(self):
self.mox.StubOutWithMock(urllib2, "urlopen")
test_file = open("testfile")
urllib2.urlopen(mox.IgnoreArg()).AndReturn(test_file)
self.mox.ReplayAll()
feedback = self.httpstorage.download(test_input, test_output)
self.assertEqual(feedback, True)
您可以嘗試使用這種格式來測試您的下載功能,因為urllib2.openurl()已被模擬進行單元測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.