Latest news

Writing Integration Tests for a Botfuel Dialog Chatbot

No comments

Since a Botfuel Dialog chatbot is a web application, you can rely on powerful programming concepts like unit and integration testing to test your bot.

In this tutorial you’ll learn how to test your chatbot using Jest, a testing library developed by Facebook. However, any other testing library such as Mocha can be used.

The goal of this tutorial is to learn how to automate the tests you would have to do manually to make sure your chatbot works as expected.

For this tutorial, we’ll start from the starter sample bot. Make sure you have it running by following the Getting Started tutorial up until the installation part.

For instance, the starter sample bot has the following behavior:

Let’s write a test that ensures this simple functionality works.

First, we need to install Jest as a development dependency:

npm install --dev jest

Once Jest is installed, add the following test script to your package.json file:

You can now run npm test to run Jest.

To run the chatbot in test mode, we’ll need a config file that indicates we’ll use the test adapter. Create a test-config.js file at the root level with this content:

To simulate a conversation between a human and your chatbot, we’ll be using the play method of the Bot class. This method expects an array of user messages that represents all the messages a user would send to the chatbot. For a user, they can be of the following types:

  • PostbackMessage
  • UserImageMessage
  • UserTextMessage

In our case, they are simple UserTextMessages.

For example, to simulate this conversation:

We’ll write:

The captured output of both the user messages and the bot messages is stored into bot.adapter.log, so we can write the following assertion to test that the bot responds as expected:

To compare expected output and actual output, we provide a convenient toJson method so messages can be compared as raw JSON. This method needs userId as a parameter because each UserTextMessage converted to JSON stores a userId property tracking the author of the message.

Here, you can also use all types of messages possible for both user and bot.

Let’s write the complete test case!

Create a tests directory at the root level of your chatbot and a hello.test.js file inside it with the following content:

To run the tests, 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’s credentials are needed because your chatbot calls NLP APIs.

You will see the following results:

Congratulations, you have successfully automated a test case of your chatbot!

Making your bots rely on APIs that are outside of your control make them dependent on the status of these APIs (downtime, API changes) and thus can fail even though you did not change anything in your bot. To make them unit tests (independent of anything other than your code, as opposed to integration tests), you will need to mock the called APIs, which will be the topic of a future article.

magnoliaWriting Integration Tests for a Botfuel Dialog Chatbot

Related Posts

Leave a Reply