Reputation: 571
What is the proper way to unit-test and integration-test my newly built constructs?
What should be actually tested? How should it be tested?
Thanks!
Upvotes: 16
Views: 12424
Reputation: 844
As far as unit tests are concerned, we've got a library (currently only in TypeScript) to make assertions against synthesized CloudFormation templates. It's called @aws-cdk/assert. So what we usually do in unit tests is define a stack, add our construct, interact with it and then make assertions against the synthesized template using the assertion library.
Here's a sketch:
import { expect, haveResource } from '@aws-cdk/assert';
import cdk = require('@aws-cdk/cdk');
const stack = new cdk.Stack();
const myConstruct = new MySpecialBucket(stack, 'Test');
myConstruct.doSomething();
expect(stack).to(haveResource('AWS::S3::Bucket', {
Prop: 1234
});
You can find many examples in the AWS CDK GitHub repository (look for "test" directories).
Integration tests are a bit more tricky. What we've been doing is basically write little CDK apps as integration tests (for example) and compare the result of cdk synth
to a checked-in expectation file. If they differ, we fail the build and request that the user manually deploy the app and update the checked-in file. This approach stems from the assumption that if the CFN template did not change, the resulting behavior would not change. That assumption has so far held quite well.
Hope that helps.
Upvotes: 22