{{announcement.body}}
{{announcement.title}}

DevOps Your Skill: Unit Tests

DZone 's Guide to

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.

· DevOps Zone ·
Free Resource

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.

Prerequisites

Here you have the technologies used in this project

  1. ASK CLI - Install and configure ASK CLI.
  2. CircleCI Account - Sign up here.
  3. Node.js v10.x.
  4. Visual Studio Code.

Alexa Skill Test Framework

We are going to use the npm package ask-sdk-test, which is the newest version of the npm package alexa-skill-test-framework.

This new version is built in TypeScript and is really easy to use in both JavaScript and TypeScript Alexa Skills.

This framework makes it easy to create full-coverage black box tests for an Alexa skill using Mocha.

Installation

You can install ask-sdk-test using npm. --save-dev is used to save the package for development purpose. Example: linters, minification.

Shell


This framework uses the npm package mocha to run the tests. So we need to install it as well:

Shell


Writing Tests

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 helloworld-tests.js in lambda/cutom/test folder.

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:

JavaScript


With the lines above, now we can start writing unit tests as easy as this:

JavaScript


Reports

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:

Shell


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:

Shell


This is how this report looks like:

CircleCI report

Finally, in order to generate two reports with just one command, we will install another npm package: mocha-multi-reporters

Shell
 




xxxxxxxxxx
1


1
npm install mocha-multi-reporters --save-dev



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 lambda/custom folder:

JSON


The JUnit report will be stored in lambda/custom/reports/mocha/ folder and the HTML one will be stored in lambda/custom/mochawesome-report.

Integration

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:

  1. test: this command will execute the unit tests and generates the reports:
    • mocha --reporter mocha-multi-reporters --reporter-options 
      configFile=mocha.json

Pipeline Job

Everything is fully installed, configured and integrated, let's add it to our pipeline!

This job will execute the following tasks:

  1. Restore the code that we have downloaded in the previous step in /home/node/project folder
  2. Run npm run test to execute the unit tests.
  3. Store the JUnit report as CircleCi test artifacts.
  4. Store the HTML report as an CircleCi artifact of this job.
  5. Persist again the code that we will reuse in the next job
YAML


Resources

Conclusion

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.

Happy coding!

Topics:
alexa, alexa skill, alexa skill development, alexa skills, alexa skills developer, alexa skills development, circleci, devops, unit test

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}