Unit testing Alexa skill that uses alexa-sdk
Asked Answered
H

5

9

I'm developing an Alexa skill in node and I'd like to know how I can unit test my code. I'm using the alexa sdk that Amazon released.

I've found many libraries to accomplish this, but they seem to be developed before the alexa sdk was available.

Thanks in advance.

Hecate answered 12/2, 2017 at 17:42 Comment(0)
V
5

We built our Alexa emulator specifically for the purpose of allowing easy unit-testing and functional-testing of Alexa skills:
http://docs.bespoken.tools/en/latest/tutorials/tutorial_bst_emulator_nodejs/

With it, you can make calls like this:

alexa.launched(function (error, response) {
    alexa.spoken('About the podcast', function (error, response) {
        assert.equal(response.response.outputSpeech.ssml, '<speak> Some SSML </speak>');
        done();
    });
});

This test code imitates a user launching the skill and saying "About the Podcast". These interactions are automatically translated to the correct Alexa JSON requests, which in turn are then sent to your skill code.

You can also create more sophisticated unit-tests that rely on mimicking the internal state of Alexa device across interactions. These are described in the tutorial.

Vidovic answered 13/2, 2017 at 2:50 Comment(0)
K
0

I am using alexa-skill-test-framework with mocha for generating the alexa intent json. Can setup AWS services in local PC using localstack, which supports DynamoDB, SQS, Lambda and other services

Katerinekates answered 5/12, 2017 at 10:13 Comment(0)
P
0

lex-bot-tester is a framework and tool to create conversational tests for Amazon Alexa and Lex.

Instead of using a simulated version of the Skill it uses the existing SMAPI to deal with Alexa.

The tests can be created manually or automatically generated by a tool included, named urutu. Right now, the code generation is python but the Skill implementation can be in any supported language.

After you interact with the Skill from the command line, defining the conversation, the generated code looks like this

#! /usr/bin/env python
import sys
import unittest

from lex_bot_tester.aws.alexa.alexaskilltest import AlexaSkillTest

verbose = True

class GeneratedTests(AlexaSkillTest):
    def test_book_my_trip_reserve_car(self):
        """
        Test generated by urutu on 2018-02-21 01:24:51
        """
        skill_name = 'BookMyTripSkill'
        intent = 'BookCar'
        conversation = [{'slot': None, 'text': 'ask book my trip to reserve a car', 'prompt': None},
                        {'slot': 'CarType', 'text': 'midsize',
                         'prompt': 'What type of car would you like to rent,  Our most popular options are economy, midsize, and luxury'},
                        {'slot': 'PickUpCity', 'text': 'vancouver',
                         'prompt': 'In what city do you need to rent a car?'},
                        {'slot': 'PickUpDate', 'text': 'tomorrow',
                         'prompt': 'What day do you want to start your rental?'},
                        {'slot': 'ReturnDate', 'text': 'next week',
                         'prompt': 'What day do you want to return the car?'},
                        {'slot': 'DriverAge', 'text': '25', 'prompt': 'How old is the driver for this rental?'}]
        simulation_result = self.conversation_text(skill_name, intent, conversation, verbose=verbose)
        self.assertSimulationResultIsCorrect(simulation_result, verbose=verbose)

 if __name__ == '__main__':
    unittest.main()

There is a more detailed explanation and some videos at Testing Alexa Skills — Autogenerated tests.

Physician answered 22/2, 2018 at 17:13 Comment(0)
G
0

We are using MochaJs with ChaiJs for testing our Alexa skill in nodeJs

We followed this tutorial to build our environment.

We added dev dependencies in the package.json

"devDependencies": { "aws-lambda-mock-context": "^3.1.1", "chai": "^4.1.2", "chai-as-promised": "^7.1.1", "mocha": "^5.0.4" }

And we have a test folder with couple of testing requests from Alexa Developer Console that we run our unit tests against.

our testing test/index.html

var Alexa = require('alexa-sdk');

const handlers = {
    'LaunchRequest': require('launchRequest')
    //other handlers
};

exports.handler = function(event, context, callback) {
    const alexa = Alexa.handler(event, context, callback);
    alexa.registerHandlers(handlers);
    alexa.execute();
};

To run the test, you just run this cmd

$ mocha
Giliane answered 15/5, 2018 at 17:37 Comment(0)
D
0

For Java, I created a unit test for my skill this way.

class GetMyRequestHandlerTest {

RequestHandler requestHandler = new GetMyRequestHandler();

@Test
void canHandle(){
    HandlerInput input = CreateSampleInput();
    boolean result = requestHandler.canHandle(input);
    assertTrue(result);
}

@Test
void handle() {
    HandlerInput input = CreateSampleInput();
    Optional<Response> response = requestHandler.handle(input);
    assertFalse(response.equals(null));
}

private HandlerInput CreateSampleInput()
{
    HandlerInput.Builder handlerInputBuilder = HandlerInput.builder();

    Intent intent = Intent.builder().withName(Constants.GetMyIntent)
                                    .putSlotsItem(Constants.SlotNumber, Slot.builder().withName(Constants.SlotNumber).withValue("4").build())
                                    .build();

    Request request = IntentRequest.builder().withIntent(intent).build();

    Session session = Session.builder().putAttributesItem(Attributes.STATE_KEY, Attributes.STATE_NUMBER).build();

    RequestEnvelope requestEnvelope = RequestEnvelope.builder().withRequest(request)
                                                            .withSession(session)
                                                            .build();

    HandlerInput input = handlerInputBuilder.withRequestEnvelope(requestEnvelope)
                                            .build();
    return input;

}
Defendant answered 2/1, 2019 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.