【问题标题】:moto with boto3 - Cannot build mock ELBmoto with boto3 - 无法构建模拟 ELB
【发布时间】:2016-06-15 22:40:27
【问题描述】:

我是 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)

【问题讨论】:

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


    【解决方案1】:

    好的,我得到了朋友的帮助。下面应该可以解决问题

    # # -*- 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多