Supercharged Unit Testing of Alexa skills with Postman and Newman

Written by Ayushverma8 | Published 2018/03/18
Tech Story Tags: aws | postman | lambda | api | newman

TLDRvia the TL;DR App

Lately Amazon has been working on improving the developer experience for its Alexa intelligent personal assistant. Part of this strategy Skill Management API,provides the ability to test multi-turn conversation, entity resolution, dialog management, and more features that are not supported by existing test tools.

Testing Alexa Skill with Postman supersedes the basic testing service provided by the Amazon Developer Portal.

To start, we are going to use three things to make all of this work

We need threee things to get started

If you haven’t built a skill for Alexa before, I would suggest , go through Build skill in 5 minutes and get started with core principles .

Once you gotten your Lambda function set up and running we are good to go .

Function might be looking something like this

With Amazon API Gateway, we can provide Alexa with a consistent and scalable programming interface to access endpoints in the backend.

  1. To begin with , go to AWS API Gateway and sign in to the console.
  2. First, make sure that you have chosen “New API” from the radio buttons at the top of the page.Then make a new role and choose “Lambda execution role
  3. Now you need to open the Actions button, and this time choose “Deploy API.” In the Deploy API box that appears, choose “[New Stage]” for Deployment/Test stage. For the other three values (Stage Name, Stage description, and Deployment description), you can just use the word “Test” for all of them. The values don’t matter, it’s just so that you know what the purpose of this new API is for.We gonna need this at later stage.

Sample Dashboard

4.Now Now , Copy the Invoke URL from the blue box at the top of the screen. Your API is set up and ready to roll.

  • Getting up and running with Postman API Tool
  1. If you don’t already have this Amazing tool, make sure to download Postman. but wait …

Why Postman ?

  • In this article , We will be using Postman to create and execute our unit tests against our Lambda function.Each time that you make a change to your Lambda, you should run your tests to make sure that everything is still working as expected
  • We have something Amazing called Monitors , Which will help us to save overhead in Unittesting Alexa Skills .
  • Postman helps us writing pre-request scripts and test scripts for requests , based on TDD. and hell yeah , Postman collections are Making API testing great again.
  • I work at Postman. 😜

Alright , So now you are up and running Postman hopefully.

If you need more security for each requests over API gateway then you can add AWS Signatures and Proceed.

Now you need a sample request get started with testing. You can get a Lambda request from the Service Simulator like serverless , or from your Cloudwatch logs in amazon account.

Choose body and click on “raw” radio button , which indicates we are forwarding our own JSON data to Lambda.

Raw datatype

Output will be embdded "SSML": Indicates that the output speech is text marked up with SSML. It’s a way by which alexa communicates.

You can know more about Request structure here.

At this point be sure and verified that you can successfully communicate with your Lambda, Now we can start creating a suite of tests for this request which will define units .

For Example , We are using favorite color skill, “WhatsMyColorIntent what my favorite color” should return “orange"

pm.test("Body is correct", function () {pm.response.to.have.body("orange");});

Similary when you want to check if Alexa continues the skill after user gives a response and reprompts for input , We need to check field shouldEndSession

Tests

Similarly you can define set of tests for a Alexa Skill and run it everytime you make some changes in skill. Postman will help you to know breaks when you make any unexpected change in the request or code.

This way you save much of time and spend writing code instead of boring testing.

You can also save Alexa response to Postman and write test without calling Prod API again again for one session.You can learn more here. You can run your collection in Runner and Know status of your Skill.

Monitor Skills on Postman’s cloud.

Now ,If you are CLI freak like me , you would prefer testing all of Tests from Command Line right ?

No problem , Postman has a tool called , newman , it’s a command line companion for Postman.

Install newman by npm install newman --global with NodeJS.

That’s it. Now it’s time to get hands dirty with Collections in CLI.

Export by clicking here.

Now , Run your tests in CLI with

newman run AlexaSkillTesting/test_to_check_color.json

And That’s it. See your terminal talking to Postman and Testing for you .

And Yes , Postman Monitors can surely help by keeping an eyes on response and notifies when something goes wrong. Because , We’re going to write an entire suite of unit tests for our skill so that when we make a change to something, we have the confidence that nothing has broken and Everything is 200 OK . And It Absolutely helps us in functional testing of Skill at each conversational phase. So , Cheers 🍺

Hopefully , I have helped you in testing alexa skills . Thanks for reading 🙌


Published by HackerNoon on 2018/03/18