来自Python的Boto的模块模块



我正在尝试为以下类似的类编写单元测试。

import boto
class ToBeTested:
    def location(self, eb):
        return eb.create_storage_location() 
    def some_method(self):
        eb = boto.beanstalk.connect_to_region(region, access_key, secret_key)
        location(eb)

有没有一种方法可以模拟 boto.beanstalk.connect_to_region 返回值,最后模拟 create_storage_location ?我是Python的补丁和模拟的新手,所以我不知道该怎么做。有人可以让我知道是否有办法做到这一点?

非常感谢!

这个想法是修补 connect_to_region(),以便它返回 Mock对象,然后您可以在模拟上定义所需的任何方法,例如:

import unittest
from mock import patch, Mock

class MyTestCase(unittest.TestCase):
    @patch('boto.beanstalk.connect_to_region')
    def test_boto(self, connect_mock):
        eb = Mock()
        eb.create_storage_location.return_value = 'test'
        connect_mock.return_value = eb
        to_be_tested = ToBeTested()
        # assertions

另请参见:

  • 嘲笑python的简介

希望会有所帮助。

最新更新