Python 如何使用pymox对该代码进行单元测试?
所以我安装了pymox,我想测试一下这个方法:Python 如何使用pymox对该代码进行单元测试?,python,mox,pymox,Python,Mox,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(o
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()已被模拟用于进行单元测试
您可以尝试使用这种格式来测试下载函数,因为urllib2.openurl()已被模拟用于单元测试。有点离题,但为什么要使用
pymox
?stdlibunittest
模块看起来非常充实。有点离题,但为什么要使用pymox
?stdlibunittest
模块看起来非常充实。