lex bot tester是一个库,它简化了amazon a lex a技能和aws lex bot测试的创建。

lex-bot-tester的Python项目详细描述


banner

横幅

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

您将看到与此类似的交互

test run

试运行

aws lex bot测试示例

您可能熟悉aws lex控制台中的此类测试 (此示例使用众所周知的orderflowersbot)。

test-bot

测试机器人

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类引用并调用 具有预期的DialogStateslots

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

对话

conversationconversationitems的列表。这些 conversationitems表示send->;response交互。

class ConversationItem(object):

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

也许,看看 lexbottestertests.py 澄清了这个想法。这个测试,使用相同的结构和类 创建B检查两个不同机器人的模型:orderflowers 还有预订。

运行测试

您可以从您喜爱的ide或命令行运行测试。

如果你对谈话的细节感兴趣,你可以 将--verbose选项添加到测试运行程序。

$ ./lexbottesttests.py --verbose

您将看到一个类似于前面介绍的交互。

term-output

术语输出

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在安卓 10中,如何在不使用READ_PHONE_STATE权限的情况下获取网络连接类型?   java SpringBoot OAuth2,keyClope不将映射角色返回为权限   如何创建pom。使用Eclipse的Java项目的xml   java Kotlin编译良好,但始终警告“发生了非法的反射访问操作”   模型类中未声明java Rails模型实例变量   java Hibernate@OneToMany因“集合未与任何会话关联”而失败   java对Atom感到失望。io无效标志   如何在序列化的java对象中正确引用自动连接的Springbean?   java将重复的ifelse语句修改为更干净的代码   java^和$在正则表达式中是什么意思?   java struts 2 dispatcher和tomcat 6安全性   由于某些电子邮件,java InternetAddress解析在Mime头上失败   笔划宽度转换(SWT)实现(Java,C#…)   swing需要一个创建按钮的Java方法   连接到本机库时出现java错误:E/eglCodecCommon:goldfishAddressSpaceHostMemoryLocator:ioctl\u ping设备类型=5,ret=1失败   API 22上的java 安卓外部存储权限   java获取jar特定路径作为字符串   Java中的多线程与计算