首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用pymox对这段代码进行单元测试?

如何用pymox对这段代码进行单元测试?
EN

Stack Overflow用户
提问于 2012-07-06 23:50:57
回答 1查看 859关注 0票数 1

所以我已经安装了pymox,我想测试一下这个方法:

代码语言:javascript
复制
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文档,但我想不出该怎么做。你能帮我写一些示例代码吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-01 14:03:08

代码语言:javascript
复制
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()已经被模拟用于进行单元测试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11365586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档