moto with boto3 - 无法构建模拟ELB

时间:2016-06-15 22:40:28

标签: python-2.7 amazon-web-services boto3 moto

我是moto和aws的新手,所以我试图在aws中为一个简单的ELB检查器提供一些简单的测试用例。

我在这里阅读了moto页面:

https://github.com/spulec/moto

我在这里关注boto3指南:

https://boto3.readthedocs.io/en/latest/reference/services/elb.html

但我不认为我理解如何使用moto与boto3。以下是我的代码和错误,非常感谢任何帮助。

# # -*- coding: utf-8 -*-

from .context import elb_tools

from nose.tools import set_trace; 
import unittest

from moto.elb import mock_elb
import boto3



class TestElbTools(unittest.TestCase):
    """Basic test cases."""

    def setUp(self):
        pass

    @mock_elb
    def test_check_elb_has_attached_instances(self):
        empty_elb = mock_elb
        mock_elb.describe_load_balancers()

if __name__ == '__main__':
    unittest.main()

输出:

D:\dev\git_repos\platform-health>nosetests
E
======================================================================
ERROR: test_check_elb_has_attached_instances (test_orphan_elb_finder.test_elb_tools.TestElbTools)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\apps\python\python27\lib\site-packages\moto\core\models.py", line 71, in wrapper
    result = func(*args, **kwargs)
  File "D:\dev\git_repos\platform-health\tests\unit_tests\test_orphan_elb_finder\test_elb_tools.py", line 22, in test_check_elb_has_attached_instances
    mock_elb.describe_load_balancers()
AttributeError: 'function' object has no attribute 'describe_load_balancers'

----------------------------------------------------------------------
Ran 1 test in 0.561s

FAILED (errors=1)

1 个答案:

答案 0 :(得分:0)

好的,所以我从朋友那里得到了一些帮助。下面应该做的伎俩

# # -*- coding: utf-8 -*-

from .context import elb_tools

from nose.tools import set_trace; 
import unittest

from moto.elb import mock_elb
import boto3

class TestElbTools(unittest.TestCase):
    """Basic test cases."""

    def setUp(self):
        #pass
        self.region = 'ap-southeast-2'

    @mock_elb
    def test_check_elb_has_attached_instances(self):
        elb_client = boto3.client('elb', region_name=self.region)
        set_trace()
        elb_client.describe_load_balancers()

if __name__ == '__main__':
    unittest.main()