A New Way to Test Chatbots - Botfuel Dialog

Article author: Maxime Khoy

Original link: Writing integration tests for a Botfuel Dialog chatbot

Compilation: Yixiong Translation Group Mason

Enter image description

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:

Enter image description

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 testscript to your package.jsonfile:

"scripts": {
  "start": "botfuel-run",
  "train": "botfuel-train",
  "test": "jest"
},

You can npm testrun Jest via .

In order to run the chatbot in test mode, we need a configuration file indicating which testadapter we will use. Create a test-config.jsfile 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 Botthe playmethod. 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:

Enter image description

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 toJsonmethods so that messages can be compared to raw JSON. This method takes userIdas a parameter, as each UserTextMessagecan be converted to JSON and stored userIdunder .

Here, you can also use all message types.

Let's write a complete test example!

Build a testslibrary at the heel level of the chatbot and create a hello.test.jsfile 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:

Enter image description

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. ...

Enter image description

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325239261&siteId=291194637