BrianMacIntosh / alexa-skill-test-framework

Framework for easy offline black-box testing of Alexa skills.
MIT License
62 stars 28 forks source link
alexa alexa-sdk alexa-skill alexa-skills-kit mocha test test-framework testing

Alexa Skill Test Framework

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

Here's an example of what a test might look like with the test framework.

describe("AMAZON.HelpIntent into AMAZON.CancelIntent", function(){
  alexaTest.test([
    { request: alexaTest.getIntentRequest("AMAZON.HelpIntent"), says: alexaTest.t("HELP_MESSAGE"), shouldEndSession: false },
    { request: alexaTest.getIntentRequest("AMAZON.CancelIntent"), says: alexaTest.t("STOP_MESSAGE"), shouldEndSession: true }
  ]);
});

Other Versions

There is a new version of this framework, that is optimized for version 2 of the ask-sdk and is written in Typescript. You can find it on https://github.com/taimos/ask-sdk-test
This version will continue to exist, but new features might be in the other library first

There is a Python adaptation of the framework available at https://github.com/BananaNosh/py_ask_sdk_test.

How To

Install the package as a dev dependency with npm install alexa-skill-test-framework --save-dev.

Write tests in a Javascript file and run them with Mocha. For example, if your test is at 'test/index.js', run mocha test/index.js.

For some simple examples, see the 'examples' directory.

Test Framework Documentation

alexaTest.initialize(index, appId, userId, deviceId)

Initializes the test framework. Must be called before generating requests or running any tests.

alexaTest.initializeI18N(resources)

Initializes i18n. You only need this if you use i18n in your skill, and you want to use i18n to fetch result strings to test against. You must have installed the optional dependencies i18n and i18next-sprintf-postprocessor.

alexaTest.setLocale(locale)

Changes the locale used by the test framework and the skill. Default is 'en-US'.

alexaTest.setDynamoDBTable(tableName)

Activates mocking of DynamoDB backed attributes.

alexaTest.unmockDynamoDB()

Removes the mock on the AWS SDK.

alexaTest.setExtraFeature(key, state)

Enables or disabled an optional test feature.

Current features are:

alexaTest.getLaunchRequest([locale])

Returns a LaunchRequest. The request can be passed to test (see below).

alexaTest.getIntentRequest(intentName, [slots], [locale])

Returns an IntentRequest. The request can be passed to test (see below).

alexaTest.getSessionEndedRequest(reason, [locale])

Returns a SessionEndedRequest. The request can be passed to test (see below).

addAudioPlayerContextToRequest(request, [token], [offset], [activity])

Adds an AudioPlayer context to the given request. Returns the given request to allow call chaining.

alexaTest.addEntityResolutionToRequest(request, slotName, slotType, value, [id])

Adds an entity resolution to the given request. Returns the given request to allow call chaining.

alexaTest.addEntityResolutionsToRequest(request, resolutions)

Adds multiple entity resolutions to the given request. Returns the given request to allow call chaining.

alexaTest.addEntityResolutionNoMatchToRequest(request, slotName, slotType, value)

Adds an entity resolution with code ER_SUCCESS_NO_MATCH to the given request. Returns the given request to allow call chaining.

alexaTest.test(sequence, [description])

Tests the skill with a sequence of requests and expected responses. This method should be called from inside a Mocha describe block.

The playsStream Object has the following properties:

alexaTest.t(arguments)

Forwards the request to alexaTest.i18n.t and returns the result. You must have called alexaTest.initializeI18N previously.

CallbackContext Documentation

Callback context objects are passed to callback and saysCallback in tests.

context.assert(data)

Throws an assertion error.

context.t(arguments)

Forwards the request to alexaTest.i18n.t and returns the result. Additionally, ensures the language is the language used in the request. You must have called alexaTest.initializeI18N previously.

Note About DynamoDB

If your skill uses the Alexa Skills Kit for Node.js and uses its built-in DynamoDB persistence, it may be desireable not to connect to the database during testing, since the ASK Node kit uses it to persist session attributes. For that case, the framework passes a fourth parameter of true to the handler function, which you can conditionalize on to disable the database connection.