Article author: Maxime Khoy
Original link: Writing integration tests for a Botfuel Dialog chatbot
Compilation: Yixiong Translation Group Mason
Target
In this tutorial you can learn how to test your chatbot using Jest , a testing library developed by Facebook. However, you can also use other testing libraries such as Mocha.
As for the goal of this tutorial, it is for you to learn how to automate the manual testing of the bot to ensure that the chatbot works as expected.
For this tutorial, we'll start with a simple sample robot . Make sure you have followed the getting started tutorial up to the installation section.
For example, the initial sample bot would behave like this:
Let's write a test to make sure this simple function works.
configure
First, we need to install Jest as a development base:
npm install --dev jest
Once Jest is installed, add the following test
script to your package.json
file:
"scripts": {
"start": "botfuel-run",
"train": "botfuel-train",
"test": "jest"
},
You can npm test
run Jest via .
In order to run the chatbot in test mode, we need a configuration file indicating which test
adapter we will use. Create a test-config.js
file at the root level with the following content:
module.exports = {
adapter: 'test',
};
write tests
To simulate a conversation between a human and a robot, we will use Bot
the play
method. This approach requires a set of user messages that can represent all messages that all users will send to the chatbot. For the user, it may be of the following types:
PostbackMessage
(receipt message)UserImageMessage
(User picture message)UserTextMessage
(user text message)
In our example, they are all simple UserTextMessage
.
For example, simulate this conversation:
We will write:
const bot = new Bot(config);
const userId = bot.adapter.userId;
await bot.play([
new UserTextMessage('Hello'),
new UserTextMessage('My name is Bob'),
]);
The captured output messages of the user and the bot are stored bot.adapter.log
, so we can write the following program to test whether the bot responds as expected:
expect(bot.adapter.log)
.toEqual([
new UserTextMessage('Hello'),
new BotTextMessage('Hello human!'),
new UserTextMessage('My name is Bob'),
new BotTextMessage('Nice to meet you Bob!'),
]
.map(msg => msg.toJson(userId))
);
To compare expected and actual output, we provide convenience toJson
methods so that messages can be compared to raw JSON. This method takes userId
as a parameter, as each UserTextMessage
can be converted to JSON and stored userId
under .
Here, you can also use all message types.
Let's write a complete test example!
Build a tests
library at the heel level of the chatbot and create a hello.test.js
file in it with the following content:
const { Bot, BotTextMessage, UserTextMessage } = require('botfuel-dialog');
const config = require('../test-config');
test('answers greeting and name', async () => {
const bot = new Bot(config);
const userId = bot.adapter.userId;
await bot.play([new UserTextMessage('Hello'), new UserTextMessage('My name is Bob')]);
expect(bot.adapter.log).toEqual(
[
new UserTextMessage('Hello'),
new BotTextMessage('Hello human!'),
new UserTextMessage('My name is Bob'),
new BotTextMessage('Nice to meet you Bob!'),
].map(msg => msg.toJson(userId)),
);
});
Before running the test, execute the following command:
BOTFUEL_APP_TOKEN=<the BOTFUEL_APP_TOKEN> BOTFUEL_APP_ID=<the BOTFUEL_APP_ID> BOTFUEL_APP_KEY=<the BOTFUEL_APP_KEY> npm test
Your app credentials are required here because your chatbot will call the NLP API.
You will see results like this:
So far, congratulations, you have successfully automated a chatbot test example!
Yixiong Translation Group is affiliated to Yixiong Technology. We do not produce scientific and technological essays. We are just porters between languages on GFW. Welcome to click "Yixiong Translation Group" to join us. The main line product "Beiqia" has recently launched a new version. If you want to experience the futuristic atmosphere brought by intelligent chatbots and the infinite charm of the new work experience, please click "Beiqia" to create your first team. ...