1 00:00:00,150 --> 00:00:01,860 ‫So here is a short lecture 2 00:00:01,860 --> 00:00:04,440 ‫around testing in the CDK. 3 00:00:04,440 --> 00:00:07,350 ‫Well, because you are using code with CDK 4 00:00:07,350 --> 00:00:09,060 ‫you can actually test your code the same way 5 00:00:09,060 --> 00:00:11,670 ‫you would test normal standard Python code 6 00:00:11,670 --> 00:00:13,890 ‫or JavaScript code, and so on. 7 00:00:13,890 --> 00:00:16,350 ‫So in the CDK apps you have what's called 8 00:00:16,350 --> 00:00:18,420 ‫the CDK assertion modules, 9 00:00:18,420 --> 00:00:20,520 ‫which will contain popular test frameworks 10 00:00:20,520 --> 00:00:24,030 ‫such as Jest for JavaScript or Pytest for Python. 11 00:00:24,030 --> 00:00:26,580 ‫And with this assertion modules, 12 00:00:26,580 --> 00:00:30,180 ‫we can verify if specific resources are what we need 13 00:00:30,180 --> 00:00:33,390 ‫or rules or conditions or parameters, and so on. 14 00:00:33,390 --> 00:00:37,890 ‫So here is a simple test where we are going to have a look 15 00:00:37,890 --> 00:00:40,920 ‫whether or not this synthesizes correctly. 16 00:00:40,920 --> 00:00:42,780 ‫That means that the CloudFormation template 17 00:00:42,780 --> 00:00:45,180 ‫that comes out of it contains what we need. 18 00:00:45,180 --> 00:00:48,240 ‫So there are two types of tests in CDK. 19 00:00:48,240 --> 00:00:50,730 ‫The first one is called a fine-grained assertion 20 00:00:50,730 --> 00:00:52,050 ‫which is the most common, 21 00:00:52,050 --> 00:00:53,940 ‫where you're going to test whether or not 22 00:00:53,940 --> 00:00:58,080 ‫some specific resources have some specific properties. 23 00:00:58,080 --> 00:01:00,420 ‫For example, in here we test whether or not 24 00:01:00,420 --> 00:01:03,390 ‫our Lambda function has a proper handler 25 00:01:03,390 --> 00:01:08,010 ‫and has a runtime of nodejs14.x. 26 00:01:08,010 --> 00:01:09,660 ‫And then we look at whether or not 27 00:01:09,660 --> 00:01:13,620 ‫our SNS topic subscription is only one, okay? 28 00:01:13,620 --> 00:01:15,900 ‫So this is fine-grained assertions. 29 00:01:15,900 --> 00:01:17,790 ‫And then we have snapshot tests 30 00:01:17,790 --> 00:01:19,680 ‫where we test the CloudFormation template 31 00:01:19,680 --> 00:01:23,250 ‫against a previously stored baseline templates, 32 00:01:23,250 --> 00:01:25,860 ‫for whatever reason, it's called a snapshot test. 33 00:01:25,860 --> 00:01:27,510 ‫So, this is very interesting 34 00:01:27,510 --> 00:01:29,490 ‫because now you can make sure that, of course, 35 00:01:29,490 --> 00:01:31,650 ‫your dynamic-db table is still here 36 00:01:31,650 --> 00:01:33,870 ‫with some, I don't know, common properties. 37 00:01:33,870 --> 00:01:35,370 ‫Whatever you want really. 38 00:01:35,370 --> 00:01:37,500 ‫And how do we test the templates? 39 00:01:37,500 --> 00:01:38,910 ‫We have two options. 40 00:01:38,910 --> 00:01:42,870 ‫The first one is to do template fromStack, MyStack, 41 00:01:42,870 --> 00:01:44,400 ‫and this is what we do right here 42 00:01:44,400 --> 00:01:48,750 ‫where we actually import a stack that we defined in CDK 43 00:01:48,750 --> 00:01:51,360 ‫and so we do templates fromStack, MyStack, 44 00:01:51,360 --> 00:01:52,500 ‫but the other way is 45 00:01:52,500 --> 00:01:56,580 ‫if your template does not live in CDK yet, 46 00:01:56,580 --> 00:02:01,260 ‫you can do an import by doing template.fromstring. 47 00:02:01,260 --> 00:02:03,840 ‫MyString in the string is your file. 48 00:02:03,840 --> 00:02:06,780 ‫And the stack is going to be built outside of CDK 49 00:02:06,780 --> 00:02:09,330 ‫so your CloudFormation template is outside of CDK 50 00:02:09,330 --> 00:02:11,130 ‫and you can still run test against 51 00:02:11,130 --> 00:02:13,530 ‫an external CloudFormation templates. 52 00:02:13,530 --> 00:02:16,110 ‫And these two commands, 53 00:02:16,110 --> 00:02:18,390 ‫these two methods are very important 54 00:02:18,390 --> 00:02:20,160 ‫to remember from an exam standpoint. 55 00:02:20,160 --> 00:02:22,950 ‫So fromStack and fromString. 56 00:02:22,950 --> 00:02:24,120 ‫All right, that's it for this lecture. 57 00:02:24,120 --> 00:02:25,320 ‫I hope you liked it. 58 00:02:25,320 --> 00:02:27,303 ‫And I will see you in the next lecture.