I'm trying to write my unit tests for a Lambda function that communicates with DynamoDB. I'm using moto but it isn't mocking anything. Whenever I call something in boto3, it communicates using my AWS CLI profile to the actual API and not a mock one. Why is this happening?
Here's the code:
### Unit test for the visitorCounterLambda function
from visitorCounterLambda import handler
import boto3
from moto import mock_dynamodb2
def setUp(self):
#pass
self.region = 'us-east-2'
@mock_dynamodb2
def test_handler():
dynamodb = boto3.client('dynamodb')
ddbTableName = "myDDBtable"
# table = dynamodb.create_table(
# TableName = ddbTableName,
# BillingMode='PAY_PER_REQUEST',
# AttributeDefinitions=[
# {
# 'AttributeName': 'id',
# 'AttributeType': 'S'
# },
# ],
# KeySchema=[
# {
# 'AttributeName': 'id',
# 'KeyType': 'HASH'
# },
# ]
# )
tablesListed = dynamodb.list_tables()
print(tablesListed)
if __name__ == '__main__':
test_handler()
print(tablesListed) returns my actual tables from my actual account. If I uncomment the create_table command, it creates the table in my AWS account as well.
What am I missing here? Thanks
In my humble opinion: stay away from moto. Each and every single version comes with other issues.
Rewritting the whole test suite to go out of such a dependency is a pain, and of course always come at the wrong time, Murphy's law.
Mock AWS dependencies yourself for unit testing, and rely on integration tests to confirm the whole thinkg is working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With