Skip to main content

banner # lex-bot-tester AWS Lex Bot Tester is a library that simplifies the creation of AWS Lex Bot tests.

Using AWS Lex Models Client this utility inspects the properties of the available Bots and creates specific Results classes to be used by the tests.

Certainly, there are ways of testing your bots using AWS CLI as explained in Test the Bot Using Text Input (AWS CLI) but lex-bot-tester provides a more concise, type safe and object oriented way of doing it.

Installation

Run

pip install lex-bot-tester

Example

You may be familiar with this kind of tests in the AWS Lex Console (this example uses the well know OrderFlowers bot).

test-bot

test-bot

More information about these manual tests using the console can be found here

However, once you have the lex-bot-tester installed, you can create tests like this one:

#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
    Lex Bot Tester
    Copyright (C) 2017  Diego Torres Milano

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
import re
import unittest

from com.dtmilano.aws.lex.conversation import Conversation, ConversationItem
from com.dtmilano.aws.lex.lexbottest import LexBotTest
from com.dtmilano.aws.lex.lexmodelsclient import LexModelsClient
from com.dtmilano.aws.lex.lexruntimeclient import DialogState

RE_DATE = re.compile('\d+-\d+-\d+')

BOT_NAME = 'OrderFlowers'
BOT_ALIAS = 'OrderFlowersLatest'
USER_ID = 'ClientId'


class OrderFlowersTests(LexBotTest):
    def test_conversations_text(self):
        lmc = LexModelsClient(BOT_NAME, BOT_ALIAS)
        conversations = []
        for i in lmc.get_intents_for_bot():
            r = lmc.get_result_class_for_intent(i)
            if i == 'OrderFlowers':
                conversations.append(Conversation(
                    ConversationItem('I would like to order some roses',
                                     r(DialogState.ELICIT_SLOT, flower_type='roses')),
                    ConversationItem('white', r(DialogState.ELICIT_SLOT, flower_type='roses', flower_color='white')),
                    ConversationItem('next Sunday',
                                     r(DialogState.ELICIT_SLOT, flower_type='roses', flower_color='white',
                                       pickup_date=RE_DATE)),
                    ConversationItem('noon', r(DialogState.CONFIRM_INTENT, flower_type='roses', flower_color='white',
                                               pickup_date=RE_DATE, pickup_time='12:00')),
                    ConversationItem('yes', r(DialogState.FULFILLED, flower_type='roses', flower_color='white',
                                              pickup_date=RE_DATE, pickup_time='12:00')),
                ))
            elif i == 'Cancel':
                conversations.append(Conversation(
                    ConversationItem('Cancel', r(DialogState.READY_FOR_FULFILLMENT))
                ))
        self.conversations_text(BOT_NAME, BOT_ALIAS, USER_ID, conversations)


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

This test, first creates a LexModelsClient to inspect the definitions of the bot, its intents and slots to later use a class factory that defines specific classes for each intent which are obtained by get_result_class_for_intent(i).

This result class reference, which extends ResultBase class is assigned to the variable r for convenience. Then, for each intent, a Conversation, consisting of a list of ConversationItems is created.

ConversationItem specifies the text or utterance sent and the expected result, using the r class reference and invoking the constructor with the expected DialogState and the values of the slots.

pickup_date is a particular case, as it is selected as next Sunday so instead of looking for a particular value we are checking if it matches a regular expression defining dates.

Finaly, once the conversation list is completed, a call to the helper method conversations_text providing this list as an argument completes the test.

Result classes

As mentioned before, LexModelsClient.get_result_class_for_intent(intent) returns the class that represents the response result once the Bot is invoked using the corresponding utterance.

The signature of the constructor matches this pattern

class MyIntentResult(ResultBase):
    def __init__(dialog_state, **kwargs):
        ...

To comply with PEP 8, keyword args representing slots are named using snake case when usually slots are named using camel case. Then, for example, the slot FlowerType will be represented by its corresponding keyword arg flower_type.

Conversations

Conversation is a list of ConversationItems. These ConversationItems represent the send -> response interaction.

class ConversationItem(object):

    def __init__(self, send, receive):
        ...

Perhaps, taking a look at lexbottestertests.py clarifies the idea. That test, uses the same structure and the classes created by inspecting the models for two different Bots: OrderFlowers and BookTrip.

Resources

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

lex_bot_tester-0.9.6-py2.py3-none-any.whl (16.8 kB view details)

Uploaded Python 2Python 3

lex_bot_tester-0.9.6-py2.7.egg (25.9 kB view details)

Uploaded Egg

File details

Details for the file lex_bot_tester-0.9.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for lex_bot_tester-0.9.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7d20fb5d4b125f16d81ae9b159e46cbc5a6980b7374d48f61d46e04c9a4ac3bd
MD5 1ea0cbe9aa5692cb545ba0f922c79694
BLAKE2b-256 567387cdb7329202af6c68e4b42030bd394cde0adfe7cb36ad132a39fe8cd823

See more details on using hashes here.

File details

Details for the file lex_bot_tester-0.9.6-py2.7.egg.

File metadata

File hashes

Hashes for lex_bot_tester-0.9.6-py2.7.egg
Algorithm Hash digest
SHA256 02a527daa43b1898e2709a4e8379eb0519f07df8dfa8293ae56dd52ae70f901e
MD5 53210090fde0c0910d02d183e0e2d797
BLAKE2b-256 227581f06287a92f53980b07146426a756b647bd92ecf8898d3b003e4c1fb8de

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.6

2 files

1.0.4

2 files

1.0.2

2 files

1.0.1

2 files

0.9.11

2 files

0.9.10

2 files

0.9.9

2 files

0.9.8

2 files

0.9.7

2 files

This release

0.9.6 This release

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page