【问题标题】:Teardown issue using Moto DynamoDB and unittest in Python 3.8在 Python 3.8 中使用 Moto DynamoDB 和 unittest 的拆解问题
【发布时间】:2021-01-05 12:13:18
【问题描述】:

我正在尝试使用基于 muhannad0 博客中的 this examplemotounittest 运行一些单元测试。

下面是我的代码(缩小了表的创建,因为它不是那么重要)。

@mock_dynamodb2
class TestDatabaseFunctions(unittest.TestCase):

    def setUp(self):
        """
        Create database resource and mock table
        """
        print("Setting up")
        self.dynamodb = boto3.resource('dynamodb', region_name='eu-west-2')

        self.table = self.dynamodb.create_table(TableName='test-table', KeySchema=[{'AttributeName': 'pk', 'KeyType': 'HASH'}, {'AttributeName': 'sk','KeyType': 'RANGE'}], AttributeDefinitions=[{'AttributeName': 'pk', 'AttributeType': 'S'},{'AttributeName': 'sk', 'AttributeType': 'S'}], ProvisionedThroughput={'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1})

        print("Waiting until the table exists")
        # Wait until the table exists.
        self.table.meta.client.get_waiter('table_exists').wait(TableName='test-table')
        assert self.table.table_status == 'ACTIVE'

        print("Ready to go")

    def tearDown(self):
        """
        Delete database resource and mock table
        """
        print("Tearing down")
        self.table.delete()
        self.dynamodb = None
        print("Teardown complete")

    def test_table_exists(self):
        """
        Test if our mock table is ready
        """
        print("Testing")
        self.assertIn('test-table', self.table.name)
        print("Test complete")


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

当我运行它时,我在 Python 3.8 的 mock.py 文件深处发现了一个错误。

=================================== FAILURES ===================================
___________________ TestDatabaseFunctions.test_table_exists ____________________
../../../env/lib/python3.8/site-packages/moto/core/models.py:102: in wrapper
    self.stop()
../../../env/lib/python3.8/site-packages/moto/core/models.py:86: in stop
    self.default_session_mock.stop()
../../../env/lib/python3.8/site-packages/mock/mock.py:1563: in stop
    return self.__exit__(None, None, None)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <mock.mock._patch object at 0x1128d2a30>, exc_info = (None, None, None)

    def __exit__(self, *exc_info):
        """Undo the patch."""
>       if self.is_local and self.temp_original is not DEFAULT:
E       AttributeError: '_patch' object has no attribute 'is_local'

../../../env/lib/python3.8/site-packages/mock/mock.py:1529: AttributeError

感谢我的穴居人调试,我可以说这个错误发生在Teardown complete 消息之后,这表明这是在测试成功执行之后发生的,并且仅在我的拆解块完成后才会发生。

我非常喜欢 muhannad0 的这个示例,因为它使用了类装饰器,让我们可以轻松地在其中模拟 dynamodb,因此希望我们可以保留它。

如果您需要更多信息,请告诉我,您应该能够通过将代码复制并粘贴到您首选的 IDE 中并点击 go 自行运行。

【问题讨论】:

    标签: python amazon-dynamodb boto3 python-3.8 moto


    【解决方案1】:

    这似乎是由于 moto 和 mock 库之间的不兼容。这个问题正在这里讨论:https://github.com/spulec/moto/issues/3535

    临时解决方法是将这些库修复为兼容的版本。我目前正在将 mock 修复为 4.0.2 以避免出现 AttributeError:

    pip install mock==4.0.2

    【讨论】:

    • 谢谢你!这也适用于 pip 吗?
    • 是的,这是我打错了,我现在已经编辑了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    相关资源
    最近更新 更多