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 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
- Create a new project folder (
- Navigate into that new folder (
- Initialize npm (
- 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.jsinstead. 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 fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (emptyproject)
entry point: (index.js)
test command: node test/testflow.js
Now you have node.js setup for the project.
- Clone testflow repository into your project at the ‘test’ folder. (If this project is also a git project itself, I recommend doing
git submodule addinstead of
- 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 firstname.lastname@example.org:robm26/testflow.git test
sed -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.
In case where you are taking in variables through utterances, you can do something like this :
DoSomething Input1=userInput1 Input2=userInput2
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.
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.
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’.
node_modules folder then upload the resulting zip file.
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!
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.