botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Better documentation for E2E and unit testing Node-based bots

Open anweiss opened this issue 9 years ago • 6 comments

Right now, there doesn't seem to be a good way to develop unit tests and end-to-end/acceptance tests against bots built using the Node.js SDK. The Emulator is great, but it would be nice to automate conversations as part of a CI process. Would be great if some documentation could be provided.

anweiss avatar Jan 04 '17 16:01 anweiss

I found the tests included in the SDK were enough for me to get started.

BotBuilder/Node/core/tests/

boardofchris avatar Jan 11 '17 18:01 boardofchris

Could you elaborate and give examples of how you setup testing for your bot?

suvamsh avatar Jan 18 '17 21:01 suvamsh

Specifically, this test was very helpful setting up testing: https://github.com/Microsoft/BotBuilder/blob/master/Node/core/tests/Dialogs.js

Line 29 sets up the bot side in the example. I created the '/' dialog in my test and called the named dialogs from my bot.

Line 61 is where you respond as the user to the bot. You have to keep track of where you are in the conversation.

Here is the basic shape of the tests I use

"use strict";

var builder = require ("botbuilder");
var connector = new builder.ConsoleConnector ();
var bot = new builder.UniversalBot (connector);
var expect = require('chai').expect;

var botUnderTest = require ('../bot/soupBot.js');

describe ('soupBotTest', function () {
    this.timeout(3000); 

	let step = 0;
    
    before (function (done) {
    	// Stuff
    });

	beforeEach (function () {
		// Stuff
		connector.processMessage ('hi'); // Kick off a reactive conversation with the first user message 
	});

    bot.dialog ('/', [
        function (session) {
            console.log ('========  ======== ====%s==== ======== ========', step);
			// Check bot state through session here
            session.beginDialog ('dialogName', args); // dialog to test
        }
    ]);

    it ('should do something', function (done) {
        bot.on ('send', function (message) {
            console.log ('--------  -------- ----%s---- -------- --------', step+1);
            if (step < 100) {
                switch (++step) {
                case 1 :
                    expect (message.text, "Step "+step).to.have.string ("Hello I am a bot"); // What you expect the bot to say
                    connector.processMessage ("Yes"); // What the user says in response
                    break;
                case 2 :
                    connector.processMessage ("fun times");
                    break;
                case 3 :
                    expect (message.text, "Step "+step).to.have.string ("Bot says something important");
                    connector.processMessage ("great");
                    break;                    
                default :
                    step = 100;
                    done ();
                    break;
                }
            }
        }) 
    }); 

    it ('should do something more...', function (done) {
        bot.on ('send', function (message) {
            if (step < 200) {
                switch (++step) {
                case 101 :
                    expect (message.text, "Step "+step).to.have.string ("You again");
                    connector.processMessage ("what's for lunch");
                    break;
                case 102 :
                    expect (message.text, "Step "+step).to.have.string ("no soup for you");
                    connector.processMessage ("8(");
                    break;
                default:
                    step = 200;
                    done ();
                    break;
                }
            }
        }) 
    }); 
});

boardofchris avatar Jan 19 '17 07:01 boardofchris

we're planning to add a better overall story for unit testing your bot but as stated above the current guidance would just be to follow the sample above.

Stevenic avatar Feb 22 '17 23:02 Stevenic

Closing for now but please feel free to comment.

Stevenic avatar Feb 22 '17 23:02 Stevenic

@anweiss I had the exact same issue. I found the looping through a switch non-intuitive and difficult to follow for larger dialog flows. To help, I made a testing framework that easily tests bot dialogs and session state. E.g.

    it('can inspect session state', () => {
        bot.dialog('/', [(session) => {
                new botbuilder.Prompts.text(session, 'What would you like to set data to?');
            }, (session, results) => {
                session.userData = { data: results.response };
                session.save();
            }]);

        return new BotTester.BotTester(bot)
            .sendMessageToBot('Start this thing!', 'What would you like to set data to?')
            .sendMessageToBot('This is data!')
            .checkSession((session) => {
                chai.expect(session.userData).not.to.be.null;
                chai.expect(session.userData.data).to.be.equal('This is data!');
        })
            .runTest();
    });

check it out! https://github.com/microsoftly/BotTester

microsoftly avatar Jul 12 '17 21:07 microsoftly