我正在尝试使用moto
并unittest
基于muhannad0 博客中的此示例运行一些单元测试。
下面是我的代码(缩小了表创建,因为它不是那么重要)。
@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 来自己运行它。