CDK Integration Testing Constructs (Alpha)
The `aws-cdk-integ-tests-alpha` library provides constructs for defining and validating integration tests for AWS CDK applications. It allows developers to deploy real AWS resources, run assertions against them, and manage their lifecycle. As an 'alpha' module, its APIs are experimental and subject to non-backward compatible changes or removal in future versions, not adhering to semantic versioning. The current version is 2.248.0a0, following the rapid release cadence of the AWS CDK.
Warnings
- breaking This library is an 'alpha' module, meaning its APIs are experimental and under active development. It is subject to non-backward compatible changes or removal in any future version and does not adhere to the Semantic Versioning model. Breaking changes will be announced in release notes.
- gotcha Executing integration tests requires the `@aws-cdk/integ-runner` CLI, which is a separate Node.js package installed via `npm`. Python users might incorrectly assume a pure Python execution environment.
- gotcha Integration tests deploy actual AWS resources to your account, incurring potential costs and requiring proper cleanup. While `integ-runner` handles cleanup by default, options like `--no-clean` can leave resources behind.
- gotcha The integration tests expect interactions with AWS SDK v3. Older implementations relying on AWS SDK v2 within constructs might behave unexpectedly or require migration.
- gotcha Initial runs of integration tests will often fail snapshot verification because no previous snapshot exists. This is expected behavior as the runner creates the first snapshot.
- gotcha Integration tests should always be run in a dedicated, isolated AWS account to prevent accidental modifications or resource conflicts with development or production environments.
Install
-
pip install aws-cdk-integ-tests-alpha -
npm install -g @aws-cdk/integ-runner
Imports
- IntegTest
from aws_cdk.integ_tests_alpha import IntegTest
- ExpectedResult
from aws_cdk.integ_tests_alpha import ExpectedResult
- Match
from aws_cdk.integ_tests_alpha import Match
Quickstart
import os
from aws_cdk import App, Stack
from aws_cdk.aws_s3 import Bucket
from aws_cdk.integ_tests_alpha import IntegTest, ExpectedResult
class MyTestStack(Stack):
def __init__(self, scope: App, id: str):
super().__init__(scope, id)
self.bucket = Bucket(self, "MyTestBucket")
app = App()
stack_under_test = MyTestStack(app, "IntegTestStack")
integ_test = IntegTest(app, "Integ",
test_cases=[stack_under_test],
# Optional: allow the test runner to destroy specific resource types
allow_destroy=['AWS::S3::Bucket']
)
# Assert that the S3 bucket exists by calling the S3 ListBuckets API
# and matching the bucket name token. The `get_att` is crucial for
# referencing runtime-generated resource attributes.
integ_test.assertions.aws_api_call(
service="S3",
api="listBuckets",
output_paths=["Buckets"]
).expect(
ExpectedResult.object_like({
"Buckets": ExpectedResult.array_with([
ExpectedResult.object_like({"Name": integ_test.get_att(stack_under_test.bucket.bucket_name).token})
])
})
)
# To run this integration test, save the above as `my_integ_test.py`,
# then synthesize with `cdk synth` and execute with the `integ-runner` CLI:
# cdk synth
# integ-runner --directory cdk.out --test-case IntegTestStack/Integ --force-update