How to write a unit test for Bot Framework v4

I’ve previously written a quick post showing a unit test for v4 preview release. I thought I’d update it.


Unit Test a dialog we’ve written that asks for a user first and second name.

Steps for creating a unit Test

  1. Create a unit test project in your solution, I chose an MS Test project
  2. Using Nuget install Microsoft.Bot.Builder.Dialogs into the MS Test project
  3. Reference your Bot project
  4. Create a unit test, remember to make it async Task
public async Task CreatingAGoodContact()
    var convoState = new ConversationState(new MemoryStorage());

    var adapter = new TestAdapter()
        .Use(new AutoSaveStateMiddleware(convoState));

    var dialogState = convoState.CreateProperty("dialogState");

    var dialogs = new DialogSet(dialogState);
    dialogs.Add(new CreateContactDialog(null));

    await new TestFlow(adapter, async (turnContext, cancellationToken) =>
        var dc = await dialogs.CreateContextAsync(turnContext, cancellationToken);
        await dc.ContinueDialogAsync(cancellationToken);
        if (!turnContext.Responded)
            await dc.BeginDialogAsync("CreateContactDialog", null, cancellationToken);
    .Send("Say something to start test")
    .AssertReply("What is their first name?")
    .AssertReply("What is their last name?")
    .AssertReply($"I have created a contact called Jane Tan")

Also see; Automate Bot Tests with saved transcript files

One thought on “How to write a unit test for Bot Framework v4

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s