lex bot tester是一个库,它简化了amazon a lex a技能和aws lex bot测试的创建。
lex-bot-tester的Python项目详细描述
横幅
Lex Bot测试仪
aws lex bot tester是一个库,它简化了 amazon alexa skills和aws lex bot功能测试。
虽然这个框架是用python开发的(node version in 路线图)它可以用来测试在任何 语言。
使用aws lex models client此实用程序检查 可用的bot并创建特定的结果类,以供 测试
当然,有一些方法可以使用aws cli作为 在Test the Bot Using Text Input (AWS CLI)中解释 但是lex bot tester提供了一个更简洁、类型安全的对象 有方向的做事方式。
安装
运行
pip install lex-bot-tester
示例
Alexa技能测试示例
本例使用alexa skill blueprint:bookmytip。
使用lex bot tester像下面这样的对话测试可以 用最少的努力创造。
classAlexaSkillManagementClientTests(AlexaSkillTest):deftest_book_my_trip_reserve_a_car(self):skill_name='BookMyTripSkill'intent='BookCar'conversation=[{'slot':None,'text':'ask book my trip to reserve a car'},{'slot':'CarType','text':'midsize'},{'slot':'PickUpCity','text':'buenos aires'},{'slot':'PickUpDate','text':'tomorrow'},{'slot':'ReturnDate','text':'five days from now'},{'slot':'DriverAge','text':'twenty five'},{'slot':None,'prompt':'Confirmation','text':'yes'}]self.conversation_text(skill_name,intent,conversation,verbose=True)
您可以在 alexaskillmanagementclienttests.py。
运行测试
您可以从您喜爱的ide或命令行运行测试。
如果你对谈话的细节感兴趣,你可以 将--verbose选项添加到测试运行程序。
$ ./alexaskillmamagementclienttests.py --verbose
您将看到与此类似的交互
试运行
aws lex bot测试示例
您可能熟悉aws lex控制台中的此类测试 (此示例使用众所周知的orderflowersbot)。
测试机器人
More information about these manual tests using the console can be found here
但是,一旦安装了lex bot tester,就可以创建 像这样的测试:
#! /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/>. """importreimportunittestfromlex_bot_tester.aws.lex.conversationimportConversation,ConversationItemfromlex_bot_tester.aws.lex.lexbottestimportLexBotTestfromlex_bot_tester.aws.lex.lexmodelsclientimportLexModelsClientfromlex_bot_tester.aws.lex.lexruntimeclientimportDialogStateRE_DATE=re.compile('\d+-\d+-\d+')BOT_NAME='OrderFlowers'BOT_ALIAS='OrderFlowersLatest'USER_ID='ClientId'classOrderFlowersTests(LexBotTest):deftest_conversations_text(self):lmc=LexModelsClient(BOT_NAME,BOT_ALIAS)conversations=[]foriinlmc.get_intents_for_bot():r=lmc.get_result_class_for_intent(i)ifi=='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')),))elifi=='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()
此测试首先创建一个LexModelsClient来检查 bot的定义、目的和槽,以便以后使用类 为获得的每个意图定义特定类的工厂 通过get_result_class_for_intent(i)。
这个结果类引用,它扩展了ResultBaseClass 为了方便起见,分配给变量r。然后,对于每一个意图, Conversation,由ConversationItems列表组成 创建。
ConversationItem指定发送的文本或语句以及 使用r类引用并调用 具有预期的DialogState和 slots。
pickup_date是一种特殊情况,因为它被选为 next Sunday所以我们不是在寻找一个特定的值 检查它是否与定义日期的正则表达式匹配。
最后,一旦conversation列表完成,调用 助手方法conversations_text提供此列表作为参数 完成测试。
但是,如果您更喜欢数据驱动的方法,您还可以 将会话声明为数据结构,如下所示 例子。
deftest_conversations_text_book_car(self):bot_name='BookTrip'bot_alias='BookTripLatest'user_id='ClientId'conversation_definition={'BookCar':[('book a car',DialogState.ELICIT_SLOT,{}),('L.A.',DialogState.ELICIT_SLOT,{}),('next week',DialogState.ELICIT_SLOT,{'PickUpDate':RE_WEEK}),('a month from now',DialogState.ELICIT_SLOT,{'ReturnDate':RE_DATE}),('25',DialogState.ELICIT_SLOT,{}),('economy',DialogState.CONFIRM_INTENT,{}),('yes',DialogState.READY_FOR_FULFILLMENT,{}),],'Cancel':[('cancel',DialogState.READY_FOR_FULFILLMENT,{})]}self.conversations_text_helper(bot_alias,bot_name,user_id,conversation_definition,verbose)
两种方法在功能上是相同的,因此您可以选择 一个适合你口味的。
结果类
如前所述, LexModelsClient.get_result_class_for_intent(intent)返回 类,该类表示在使用 相应的话语。
构造函数的签名与此模式匹配
class MyIntentResult(ResultBase): def __init__(dialog_state, **kwargs): ...
为了遵守PEP 8, 表示时隙的关键字参数使用snake case命名 通常,插槽使用camel case命名。然后,例如,插槽 FlowerType将由其对应的关键字arg表示 flower_type。
对话
conversation是conversationitems的列表。这些 conversationitems表示send->;response交互。
class ConversationItem(object): def __init__(self, send, receive): ...
也许,看看 lexbottestertests.py 澄清了这个想法。这个测试,使用相同的结构和类 创建B检查两个不同机器人的模型:orderflowers 还有预订。
运行测试
您可以从您喜爱的ide或命令行运行测试。
如果你对谈话的细节感兴趣,你可以 将--verbose选项添加到测试运行程序。
$ ./lexbottesttests.py --verbose
您将看到一个类似于前面介绍的交互。
术语输出