DevOps Your Skill: Unit Tests
In this article, we discuss how to add unit tests to your Alexa Skill and hoy to run it pipeline and its importance in the day-to-day Alexa Skills development.
Join the DZone community and get the full member experience.Join For Free
A unit test is a method that calls functions or pieces of code from your source code with some inputs and then validates if the output is the one that you want to expect. Thus, these tests validates if the result is the one expected. The method or function to be tested is known as System under test (SUT).
These tests are automated in the continuous integration system (CircleCI) and are executed in each new version of the software.
Here you have the technologies used in this project
- ASK CLI - Install and configure ASK CLI.
- CircleCI Account - Sign up here.
- Node.js v10.x.
- Visual Studio Code.
We are going to use the npm package
ask-sdk-test, which is the newest version of the npm package
This framework makes it easy to create full-coverage black box tests for an Alexa skill using
You can install
ask-sdk-test using npm.
--save-dev is used to save the package for development purpose. Example: linters, minification.
This framework uses the npm package
mocha to run the tests. So we need to install it as well:
Once we have the test framework installed and its dependencies now we have to write some unit tests.
You can find all the tests in the
We are going to explain step-by-step this file.
First, the initialization of the test framework and the i18n library is required to write some unit tests:
With the lines above, now we can start writing unit tests as easy as this:
Once we have everything configured, we have to set up the reports we are going to use to check the output of the unit tests.
The first report we need to set up is the JUnit report.
We need to install this output format of the report:
This report will generate a .xml file as output that CircleCI is going to use to print the lint results:
We are going to move one step forward. We want to know a little bit more about our unit tests results in every pipeline execution.
This is why we are going to add the
mochawesome npm package to generate a beautiful HTML report with more information rather than the above explained:
This is how this report looks like:
Finally, in order to generate two reports with just one command, we will install another npm package:
Then, we have to configure this npm package to specify the two reporters we are going to run ant its configuration.
The configuration file is
mocha.json file in
The JUnit report will be stored in
lambda/custom/reports/mocha/ folder and the HTML one will be stored in
Now it is time to integrate it into our
package.json to use it in our pipeline with
npm run commands!
So, in this file we are going to add the following commands in the
script json node:
test: this command will execute the unit tests and generates the reports:
mocha --reporter mocha-multi-reporters --reporter-options
Everything is fully installed, configured and integrated, let's add it to our pipeline!
This job will execute the following tasks:
- Restore the code that we have downloaded in the previous step in
npm run testto execute the unit tests.
- Store the JUnit report as CircleCi test artifacts.
- Store the HTML report as an CircleCi artifact of this job.
- Persist again the code that we will reuse in the next job
- DevOps Wikipedia - Wikipedia reference
- Official Alexa SDK Test Framework Documentation - Alexa SDK Test Framework Documentation
- Official CircleCI Documentation - Official CircleCI Documentation
The main goal of unit tests is to detect errors within individual software components. The idea is to ensure the accuracy of the code throughout the development process. It is necessary to write unit tests constantly.
You can write unit tests with Bespoken as well. See documentation here
That’s all, folks!
You can find the code in my GitHub.
I hope it will be useful! If you have any doubts or questions, do not hesitate to contact me or put a comment below.
Opinions expressed by DZone contributors are their own.