使用botium和testmybot包测试Chatbots

时间:2018-02-19 06:07:55

标签: node.js testing chatbot

如何使用botium或testmybot节点包测试聊天机器人?

我无法找到任何端到端的样本来理解这一点。

1 个答案:

答案 0 :(得分:1)

TestmybotBotium的Github存储库中包含多个示例。

Botium Wiki包含一些有用的信息和Walkthrough

运行Botium脚本的基本步骤如下(来自one of the samples

安装要求

  • 安装Node.js
  • 安装docker
  • 安装docker-compose

还有其他样品可用,不需要泊坞。

初始化Botium目录

打开命令行窗口,创建目录,初始化NPM并下载Botium包。

mkdir botium
cd botium
npm init
npm install --save botium-core

加载botium库

首先,加载botium库和必需的类。

const BotDriver = require('botium-core').BotDriver
const Capabilities = require('botium-core').Capabilities
const Source = require('botium-core').Source

配置功能

告诉Botium正在测试哪种聊天机器人以及如何连接它。在此示例中,聊天机器人应该加载到docker容器中,Botium必须挂钩到Microsoft Bot Framework。

const driver = new BotDriver()
    .setCapability(Capabilities.PROJECTNAME, 'core-CreateNewConversation')
    .setCapability(Capabilities.CONTAINERMODE , 'docker')
    .setCapability(Capabilities.BOTFRAMEWORK_API, true)
    .setCapability(Capabilities.BOTFRAMEWORK_APP_ID, 'my microsoft app id')
    .setCapability(Capabilities.BOTFRAMEWORK_CHANNEL_ID, 'facebook')

配置chatbot存储库

Botium直接从源Github存储库中检索chatbot代码。作为替代方案,可以首先克隆存储库并从本地目录加载。在CI环境中,从Git加载通常更有意义。 此外,运行示例聊天机器人需要初始化Git存储库的命令(“npm install”),启动chatbot服务的命令(“npm start”)和一些环境变量。

driver.setSource(Source.GITURL, 'https://github.com/Microsoft/BotBuilder-Samples.git')
    .setSource(Source.GITDIR, 'Node/core-CreateNewConversation')
    .setSource(Source.GITPREPARECMD, 'npm install')
    .setCapability(Capabilities.STARTCMD, 'npm start')
    .setEnv('MICROSOFT_APP_ID', 'my microsoft app id')
    .setEnv('MICROSOFT_APP_PASSWORD', 'my microsoft app password')
    .setEnv('NODE_DEBUG', 'botbuilder')
    .setEnv('DEBUG', '*')

运行对话并评估回复

Botium提供“fluent interface”。

首先,初始化Botium驱动程序(创建工作目录,下载存储库,构建docker网络......)并启动。

driver.BuildFluent()
    .Start()
    ...

然后,通过向chatbot(“UserSaysText”)发送输入或等待来自聊天机器人(“WaitBotSaysText”)的反应来开始对话。该对话是针对正在使用的聊天机制量身定制的。如果聊天机器人没有反应或出现意外反应,会话立即结束。

...
.UserSaysText('hi bot')
.WaitBotSaysText((text) => assert('You\'ve been invited to a survey! It will start in a few seconds...', text))
.WaitBotSaysText(null, 10000, (text) => assert('Hello... What\'s your name?', text))
.UserSaysText('John')
.WaitBotSaysText((text) => assert('Hi John, How many years have you been coding?', text))
.UserSaysText('5')
.WaitBotSaysText((text) => assert('What language do you code Node using?', text))
.UserSaysText('CoffeeScript')
.WaitBotSaysText((text) => assert('Got it... John you\'ve been programming for 5 years and use CoffeeScript.', text))
...

最后,停止Botium并执行一些清理任务。 不要忘记“执行”调用,否则根本不会执行任何操作!

...
.Stop()
.Clean()
.Exec()
...

运行程序并观察输出

现在在命令行窗口中照常运行程序。

[ec2-user@ip-172-30-0-104 botframework]$ node botiumFluent.js
SUCCESS: Got Expected <You've been invited to a survey! It will start in a few seconds...>
SUCCESS: Got Expected <Hello... What's your name?>
SUCCESS: Got Expected <Hi John, How many years have you been coding?>
SUCCESS: Got Expected <What language do you code Node using?>
SUCCESS: Got Expected <Got it... John you've been programming for 5 years and use CoffeeScript.>
READY
[ec2-user@ip-172-30-0-104 botframework]$

TestMyBot

Botium与Selenium / Appium所做的相当(统一API和“页面对象模型”)。 TestMyBot是Botium之上的一个层,用于将Botium与CI / CD管道和测试运行器(如Mocha和Jasmine)集成。对话不必像上面那样编码,而是在文本文件,excel文件或yml文件中“编写脚本”,例如:

urvey

#me
hi

#bot
You've been invited to a survey! It will start in a few seconds...

#bot
Hello... What's your name?

#me
John

#bot
Hi John, How many years have you been coding?

#me
10

#bot
What language do you code Node using? 

#me
C#

#bot
I didn't understand. Please choose an option from the list.

#me
JavaScript

#bot
Got it... John you've been programming for 10 years and use JavaScript.

所有这些文件都必须放在spec / convo目录中,Jasmine或Mocha(或任何其他测试运行器)的测试用例只需一个简短的scriptlet即可创建(放入spec / / testmybot.spec.js):

const bot = require('testmybot');

bot.helper.jasmine().setupJasmineTestSuite(60000);

了解Jasmine或Mocha真的很有帮助。正确设置后,唯一要运行的命令是:

npm run test