paint-brush
Test Driven Development with Alexa SDKby@binhonglee
538 reads
538 reads

Test Driven Development with Alexa SDK

by BinHong Lee February 17th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

According to <a href="https://en.wikipedia.org/wiki/Test-driven_development" target="_blank">Wikipedia</a>, it means “Requirements are turned into very specific <a href="https://en.wikipedia.org/wiki/Test_case" title="Test case" target="_blank">test cases</a>, then the software is improved to pass the new tests, only.” Basically, you would first have to write the test of the software/feature you are developing before you start working on the developing the software/feature itself.

People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Test Driven Development with Alexa SDK
BinHong Lee  HackerNoon profile picture

Photo by Andres Urena on Unsplash

Quick guide to building and unit testing an Alexa Skill

What is Test Driven Development?

According to Wikipedia, it means “Requirements are turned into very specific test cases, then the software is improved to pass the new tests, only.” Basically, you would first have to write the test of the software/feature you are developing before you start working on the developing the software/feature itself.



Here are a few more articles on the benefits and why you should apply TDD:- Test Driven Development by Jan Olbrich- Isn’t TDD twice the work? Why should you care? by Navdeep Singh

Preparation

Here are a few thing you might want to have set up or installed before getting started:

  • Amazon Web Service account (a free one will work just fine)
  • node.js (personally though, I prefer installing and maintaining it through nvm)
  • Text editor of your choice

Setting up

  1. Create a new project folder (mkdir project-name)
  2. Navigate into that new folder (cd project-name)
  3. Initialize npm (npm init)
  4. Follow through everything as you prefer (if you don’t know what it is, just press enter and it’ll keep the default) except for test command:, put in node test/testflow.js instead. The command line should look something like the snippet below.


This utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sensible defaults.


See `npm help json` for definitive documentation on these fieldsand exactly what they do.


Use `npm install <pkg>` afterwards to install a package andsave it as a dependency in the package.json file.










Press ^C at any time to quit.package name: (emptyproject)version: (1.0.0)description:entry point: (index.js)test command: node test/testflow.jsgit repository:keywords:author:license: (ISC)

Now you have node.js setup for the project.

Unit testing

Shout out to robmccauley for maintaining testflow that provides unit testing for Alexa skills. Here’s what the following commands will do :

  • Clone testflow repository into your project at the ‘test’ folder. (If this project is also a git project itself, I recommend doing git submodule add instead of git clone.)
  • Create a folder named ‘dialogs’
  • Create a file named ‘default.txt’ in the ‘dialog’ folder
  • Edit the ‘SourceCodeFile’ line in the cloned ‘testflow’ repository to point to the ‘index.js’ in the base level of the folder (feel free to edit it to your personal preference).




git clone [email protected]:robm26/testflow.git testmkdir dialogstouch dialogs/default.txtsed -i '9c const SourceCodeFile = "../index.js"' test/testflow.js

Inside the ‘default.txt’ file in the ‘dialogs’ folder, you should have the intents you are trying to test. The default in the testflow repository consist of the followings but you should definitely add some more customized ones to it depending on what you are trying to do in your skill.



LaunchRequestAMAZON.HelpIntentAMAZON.StopIntent

In case where you are taking in variables through utterances, you can do something like this :

DoSomething Input1=userInput1 Input2=userInput2

To get a deeper understanding into using testflow, I recommend looking into its tutorial page and the blog post by the author himself.

Developing the skill

To be honest, I don’t have too much to talk about on this topic. Here is some sample code of how a sample skill can look like. (Also remember to do npm install --save alexa-sdk.)

I recommend looking into the official Alexa GitHub repository that provides all kinds of sample skills that you can work on top of.

Deployment

Alexa Skill Kit

Go to this page and select ‘Create Skill’. In there you should see options to ‘add intents’ and ‘custom slot types’. You should add the appropriate slot types and intents accordingly from above. After you are done with that, select ‘Endpoint’ from the side bar and select ‘AWS Lambda ARN’ as the skill’s service endpoint. Make a copy of the Skill ID as you will need it later.

AWS Lambda

Create an AWS Lambda function selecting ‘from scratch’. After that, you should have an option to ‘add trigger’ where you would select ‘Alexa Skills Kit’. Now in the configuration dialog, you should put in the Skill ID you took a copy of previously then select ‘Add’.

After this, select back to the box that labeled your project name. There should be a ‘Function code’ dialog box appeared below. While you can definitely copy and paste your code onto the online text editor, there might be chance where the packages you have installed and used in your code is included by default. It is also possible that you have more than 1 JavaScript files for the skill.

In this case, you would select ‘Upload a .ZIP file’ under ‘Code entry type’. Locally, zip up all the JavaScript files along with the node_modules folder then upload the resulting zip file.

Submission

When you are done, you can go back to the Alexa Skill Kit page, select the ‘Distribution’ tab and fill out the appropriate information before submitting your skill for review. Hopefully everything goes well and your skill gets published at the Alexa Skill Store!

About me

I recently started working at Facebook as a Software Engineer. I spend a lot of my free time experimenting and building new things with technologies I find fun and interesting. Follow my exploration journey here or on GitHub.

Credits