Closed phubbard closed 5 years ago
Hi @phubbard
It seems like pytest fixtures don't get invoked for the setup_method()
, so the DynamoDB table is being created before the botocore clients have been patched.
Here a way that works by using a fixture instead of setup_method()
:
#!/usr/bin/env python3
# Integration tests, using localstack.
import os
import boto3
import pytest
from aws_xray_sdk.core import xray_recorder
import pytest_localstack
xray_recorder.configure(sampling=False, context_missing='LOG_ERROR')
localstack = pytest_localstack.patch_fixture(
services=['dynamodb'],
scope='module', # Use the same Localstack container for all tests in this module.
autouse=True, # Automatically use this fixture in tests.
region_name = 'us-west-2'
)
class TestIntegration:
@pytest.fixture(autouse=True)
def setup_and_teardown(self):
xray_recorder.begin_segment('integration_test')
self.ddb = boto3.resource('dynamodb')
self.table = self.ddb.create_table(TableName='audit',
KeySchema=[
{
'AttributeName': 'intuit_tid',
'KeyType': 'HASH'
},
{
'AttributeName': 'utc_timestamp',
'KeyType': 'RANGE'
}
],
AttributeDefinitions=[
{
'AttributeName': 'authid',
'AttributeType': 'S'
},
{
'AttributeName': 'action',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 5,
'WriteCapacityUnits': 5
})
yield # wait for test to complete, then teardown
xray_recorder.end_segment()
def test_audit_db(self):
assert self.table.item_count == 0
Thank you!
This code fails with this error:
Here is the test: